问题描述
在SignalR没有在HubConnectionContext这样定义公共财产:
公共动态所有{搞定;组; }
这使得用户可以调用它: All.someMethodName();
这是辉煌的。
我现在想调用这个在我的函数中使用传入的参数。我怎样才能做到这一点。
如:所有<我作为方法名称>变量;();
有没有这个做什么?
感谢
$ b $的任何方式b
修改例如:
公共无效AcceptSignal(字符串methodToCall,string信息)
{
Clients.All.someMethod(MSG); //这个工程
Clients.All< methodToCall>(MSG); //这不起作用(不过,我想它想!)
}
虽然我喜欢所有的乐趣反射的答案,还有一个更简单,更快速地调用使用一个字符串作为方法名客户中心的方法途径。
Clients.All
, Clients.Others
, Clients.Caller
, Clients.AllExcept(connectionIds)
, Clients.Group(组名)
, Clients.OthersInGrouop(组名)
和 Clients.Client(的ConnectionId)
都是动态对象,但他们也都实现了的接口。
你可以施放任何此类动态对象到 IClientProxy
,然后调用的:
公共无效AcceptSignal(字符串methodToCall,string信息)
{
IClientProxy代理= Clients.All;
proxy.Invoke(methodToCall,味精);
}
In SignalR there is public property defined in the HubConnectionContext as such:
public dynamic All { get; set; }
This enables users to call it like: All.someMethodName();
which is brilliant.
I now would like to call this using an incoming parameter in my function. How can I do this?
As in: All.<my variable as method name>();
Is there any way of doing this?
Thanks
EDIT example:
public void AcceptSignal(string methodToCall, string msg)
{
Clients.All.someMethod(msg); // THIS WORKS
Clients.All.<methodToCall>(msg); // THIS DOES NOT WORK (But I would like it to!)
}
While I love all the fun reflection answers, there's a much simpler and faster way to invoke client hub methods using a string as the method Name.
Clients.All
, Clients.Others
, Clients.Caller
, Clients.AllExcept(connectionIds)
, Clients.Group(groupName)
, Clients.OthersInGrouop(groupName)
, and Clients.Client(connectionId)
are all dynamic objects, but they also all implement the IClientProxy interface.
You can cast any of these dynamic objects to an IClientProxy
, and then call Invoke(methodName, args...):
public void AcceptSignal(string methodToCall, string msg)
{
IClientProxy proxy = Clients.All;
proxy.Invoke(methodToCall, msg);
}
这篇关于如何使用动态对象以使用一个变量作为一个方法名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!