How to use reflection to call method by name

后端 未结 4 1396
太阳男子
太阳男子 2020-11-28 13:46

Hi I am trying to use C# reflection to call a method that is passed a parameter and in return passes back a result. How can I do that? I\'ve tried a couple of things but wit

4条回答
  •  抹茶落季
    2020-11-28 13:54

    You can use Delegate.CreateDelegate to obtain a delegate to the method by name:

    public static R ResponseHelper(T request, string serviceAction)
    {
        var service = new ContentServiceRef.CMSCoreContentServiceClient();
    
        var func = (Func)Delegate.CreateDelegate(typeof(Func),
                                                      service,
                                                      serviceAction);
    
        return func(request);
    }
    

提交回复
热议问题