How can I programmatically do method overload resolution in C#?

前端 未结 1 1171
南旧
南旧 2021-02-18 15:48

When the C# compiler interprets a method invocation it must use (static) argument types to determine which overload is actually being invoked. I want to be able to do this prog

1条回答
  •  死守一世寂寞
    2021-02-18 16:04

    Answer

    Use Type.GetMethod(String name, Type[] types) to do programmatic overload resolution. Here is an example:

    MethodInfo methodToInvoke = typeof(MyClass)
        .GetMethod("myFunc", new System.Type[] { typeof(BaseClass) });
    

    Explanation

    In the example, methodToInvoke will be myFunc(BaseClass bc) not myFunc(DerivedClass dc), because the types array specifies the parameter list of the method to get.

    From the MSDN documentation, Type.GetMethod(String name, Type[] types) has two parameters:

    • name is the name of the method to get, and
    • types provides the order, number, and types of the method's parameters.

    Running Code

    Here is a running fiddle that demonstrates programmatic overload resolution.

    using System;
    using System.Reflection;
    
    public static class Program
    {
        public static void Main()
        {
            MethodInfo methodToInvoke = typeof(MyClass)
                .GetMethod("myFunc", new System.Type[] { typeof(BaseClass) });
    
            var result = methodToInvoke
                .Invoke(new MyClass(), new object[] { new BaseClass() });
    
            Console.WriteLine(result);      
        }
    
        public class MyClass
        {
            public static string myFunc(BaseClass bc) {
                return "BaseClass";
            }
    
            public static string myFunc(DerivedClass dc) {
                return "DerivedClass";
            }
        }
    
        public class BaseClass { }
        public class DerivedClass : BaseClass { }
    }
    

    The output is BaseClass.

    Edit: This is robust.

    GetMethod resolved methods that take params, more derived classes, delegates, and interface implementations. This Fiddle demonstrates all of those cases. Here are the calls and what they retrieve.

    Works with params

    MethodInfo methodToInvoke2 = typeof(MyClass).GetMethod(
            "myFunc",
            new System.Type[] { typeof(Int32[]) });
    

    will resolve this method

    public static string myFunc(params int[] i)
    {
        return "params";
    }
    

    Works with more derived classes

    MethodInfo methodToInvoke3 = typeof(MyClass).GetMethod(
        "myFunc", 
        new System.Type[] { typeof(MoreDerivedClass) });
    

    resolves a method that takes the MoreDerivedClass

    public class BaseClass { }
    public class DerivedClass : BaseClass { }
    public class MoreDerivedClass : DerivedClass {}
    

    Works with delegates

    MethodInfo methodToInvoke4 = typeof(MyClass).GetMethod(
        "myFunc", 
        new System.Type[] { typeof(MyDelegate) });
    

    ... will retrieve a method that takes this delegate:

    public delegate void MyDelegate(string x);
    

    Works with interface implementations

    MethodInfo methodToInvoke5 = typeof(MyClass).GetMethod(
       "myFunc", 
       new System.Type[] { typeof(MyImplementation) });
    

    ... successfully retrieves a method that takes MyImplementation

    public interface IMyInterface {}
    public class MyImplementation : IMyInterface {}
    

    So, it is robust, and we can use GetMethod to do overload resolution in cases that we might not expect to work.

    0 讨论(0)
提交回复
热议问题