c# - How to use a variable as a method name using dynamic objects -


in signalr there public property defined in hubconnectioncontext such:

public dynamic { get; set; } 

this enables users call like: all.somemethodname(); brilliant.

i call using incoming parameter in function. how can this?

as in: all.<my variable method name>();

is there way of doing this?

thanks

edit example:

    public void acceptsignal(string methodtocall, string msg)     {         clients.all.somemethod(msg);       // works         clients.all.<methodtocall>(msg);   // not work (but to!)     } 

while love fun reflection answers, there's simpler , faster way invoke client hub methods using string method name.

clients.all, clients.others, clients.caller, clients.allexcept(connectionids), clients.group(groupname), clients.othersingrouop(groupname), , clients.client(connectionid) dynamic objects, implement iclientproxy interface.

you can cast of these dynamic objects iclientproxy, , call invoke(methodname, args...):

public void acceptsignal(string methodtocall, string msg) {      iclientproxy proxy = clients.all;     proxy.invoke(methodtocall, msg); } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -