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

后端 未结 4 838
迷失自我
迷失自我 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 17:17

    You can use reflection to achieve this:

    Type allType = All.GetType();
    // GetType() may return null in relation to dynamics objects
    if (allType != null)
    {
        MethodInfo methodInfo = allType.GetMethod(methodToCall);
        methodInfo.Invoke(All, null);
    }
    

提交回复
热议问题