c# - Actions with variable argument count -
i'm working unity networking, , i'm writing method calls 1 on network. have bunch of methods like
void call<a>(action<a> method, arg) { _networkview.rpc(method.method.name, rpcmode.others, arg); } void call<a,b>(action<a,b> method, arg1, b arg2) { _networkview.rpc(method.method.name, rpcmode.others, arg1, arg2); }
... , on
there variants call on specific player , system make work photon cloud. there whole lot of these methods, , it's getting cluttered.
now, wondering if it's possible have action can have amount of arguments. can make like:
void call(action method, params object[] args) { _networkview.rpc(method.method.name, rpcmode.others, args); }
is possible?
thanks!
actions variable argument count
it possible:
public delegate void paramarrayaction<v>(params v[] variableargs); public delegate void paramarrayaction<f, v>(f fixedarg, params v[] variableargs); public delegate void paramarrayaction<f1, f2, v>(f1 fixedarg1, f2 fixedarg2, params v[] variableargs); public delegate void paramarrayaction<f1, f2, f3, v>(f1 fixedarg1, f2 fixedarg2, f3 fixedarg3, params v[] variableargs);
your problem
but it's not ask for. actually, asking magic delegate type method assignable. far know it's impossible.
this quite strange (or, maybe, don't understand something). far understand, have whole bunch of methods this
void call<a>(action<a> method, arg) { _networkview.rpc(method.method.name, rpcmode.others, arg); } void call<a, b>(action<a, b> method, arg1, b arg2) { _networkview.rpc(method.method.name, rpcmode.others, arg1, arg2); } ..... void call<a, b, c, d>(action<a, b, c, d> method, arg1, b arg2, c arg3, d arg4) { _networkview.rpc(method.method.name, rpcmode.others, arg1, arg2, arg3, arg4); } .....
and use them this:
[rpc] void dosomething(string sometext, int somevalue) { // bla-bla-bla } ..... void somerpccallingmethod() { // bla-bla-bla call(dosomething, "hello, world!", 42); // bla-bla-bla }
and kinda it. adds compile time check method params. good. don't understand is:
there whole lot of these methods
why? i'm not familiar unity's rpc. anyway, can:
- create separate class encapsulate
networkview
,call<>
methods. - add
rpcmode
parametercall<>
methods.
if still want it, this:
void call(string methodname, params object[] args) { _networkview.rpc(methodname, rpcmode.others, args); } usage: [rpc] void dosomething(string sometext, int somevalue) { // bla-bla-bla } ..... void somerpccallingmethod() { // bla-bla-bla call("dosomething", "hello, world!", 42); // bla-bla-bla }
Comments
Post a Comment