Invoking Member of a class using Class Name and Method Name

前端 未结 2 746
粉色の甜心
粉色の甜心 2021-01-07 05:04

I am trying to invoke function of a class using Reflection (assuming that object initialization as no dependency on Function to be invoked) like this



        
2条回答
  •  既然无缘
    2021-01-07 05:38

    To answer the question on how to cast the result to a generic return type, the method would look something like:

    public static T InvokeMethod(string assemblyName, string namespaceName, string typeName, string methodName, string stringParam)
    {
        // instead of String s = null;
        T methodResult = default(T);
    
        // instead of s = (String)calledType.InvokeMember(...)
        methodResult = (T)calledType.InvokeMember(...);
    
        // return s;
        return methodResult;
    }
    

提交回复
热议问题