Calling a static method using a Type

前端 未结 3 550
逝去的感伤
逝去的感伤 2020-11-30 12:33

How do I call a static method from a Type, assuming I know the value of the Type variable and the name of the static method?

public         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 12:56

    Note, that as 10 years have passed. Personally, I would add extension method:

    public static TR Method(Type t, string method, object obj = null, params object[] parameters) 
        => (TR)t.GetMethod(method)?.Invoke(obj, parameters);
    

    and then i could call it with

    var result = typeof(Foo1Class).Method(nameof(Foo1Class.Foo1Method));
    

提交回复
热议问题