How to invoke methods with ref/out params using reflection

后端 未结 1 1754
眼角桃花
眼角桃花 2020-12-10 04:56

Imagine I have the following class:

class Cow {
    public static bool TryParse(string s, out Cow cow) {
        ...
    }
}

Is it possible

相关标签:
1条回答
  • 2020-12-10 05:33

    You can do something like this:

    static void Main(string[] args)
    {
        var method = typeof (Cow).GetMethod("TryParse");
        var cow = new Cow();           
        var inputParams = new object[] {"cow string", cow};
        method.Invoke(null, inputParams); 
    }
    
    class Cow
    {
        public static bool TryParse(string s, out Cow cow) 
        {
            cow = null; 
            Console.WriteLine("TryParse is called!");
            return false; 
        }
    }
    
    0 讨论(0)
提交回复
热议问题