Invoke a Static Method using Reflection

class
ClassWithGenericStaticMethod
{
   
public
static void
PrintName<T>(
string
prefix)
where T :
class
   
{
        Console.WriteLine(prefix + " " + typeof
(T).FullName);
    }
}



// Grabbing the type that has the static generic
method

Type typeofClassWithGenericStaticMethod =
typeof(ClassWithGenericStaticMethod
);

// Grabbing the specific static method
MethodInfo methodInfo =
typeofClassWithGenericStaticMethod.GetMethod(
"PrintName", System.Reflection.BindingFlags.Static | BindingFlags
.Public);

// Binding the method info to generic
arguments
Type[]
genericArguments =
new
Type[] { typeof(Program
)
};
MethodInfo
genericMethodInfo =
methodInfo.MakeGenericMethod(genericArguments);

// Simply invoking the method and
passing parameters
// The null parameter is the object to call the method
from. Since the method is
// static, pass null.
object returnValue =
genericMethodInfo.Invoke(
null, new object[] { "hello" });

原文地址:https://www.cnblogs.com/adalovelacer/p/2081119.html