How to use a variable as a method name using dynamic objects

后端 未结 4 802
迷失自我
迷失自我 2021-01-02 16:16

In SignalR there is public property defined in the HubConnectionContext as such:

public dynamic All { get; set; }

This enables users to cal

4条回答
  •  醉梦人生
    2021-01-02 16:52

    public void AcceptSignal(String methodToCall, String msg) {
        var count=(
            from target in new[] { Clients.All }
            from memberInfo in ((Type)target.GetType()).GetMember(methodToCall)
            where MemberTypes.Method==memberInfo.MemberType
            let methodInfo=memberInfo as MethodInfo
            let paraInfos=methodInfo.GetParameters()
            where null!=paraInfos.FirstOrDefault(x => msg.GetType()==x.ParameterType)
            select methodInfo.Invoke(target, new object[] { msg })
            ).Count();
    }
    

提交回复
热议问题