C# How to use GetMethod to find a specific overload with a generic out parameter?

梦想的初衷 提交于 2021-02-11 13:48:47

问题


Everything is in the title, but let me put some code. Let's say we have a class with 2 methods with the same name:

class MyClass
{
  /// <summary>
  /// The first version
  /// </summary>
  public TItem Foo(long param1) { ... }
  /// <summary>
  /// Overload with a generic parameters
  /// </summary>
  public bool Foo<TItem>(out TItem item, long param1) { ... }
}

An we need to get the MethodInfo for the second 'Foo' method which has a generic out parameter:

    public class AnotherClass
    {
      public MethodInfo GetFooForType(Type typeOfItem)
      {
        // the answer is probably GetMethod with the right parameters
        return typeof(MyClass).GetMethod(???);
      }
    }

Please note that:

  • I'm trying to NOT USE GetMethods or GetMember because it's inelegant and prone-to-future-errors
  • Renaming any of the Foo methods is not an option (would be too easy).
  • The type or TItem is obviously known only at runtime.
  • I'm probably looking for the right parameters for GetMethod, but I can take any suggestion (other that GetMethods or GetMember).
  • I spent 1 hour searching StackOverflow for the answer without success, but reflection evolved so much that there is a lot of irrelevant answers all around. Adding a date filter in stackoverflow might be a great idea (or maybe a .Net framework version filter ?)
  • You are more than welcome to prove me wrong an find me the obvious answer I missed !
  • This is not a .Net Core project (MakeGenericMethodParameter is not an option)
  • My name is Mose and I'm a bullet addict ^^

回答1:


If your class is a generic one (and if it is not, your code will give you a warning about overriding) you can use typeof(MyClass<>).GetGenericArguments().First().MakeByRefType():

class MyClass<TItem>
{
    /// <summary>
    /// The first version
    /// </summary>
    public TItem Foo(long param1) { throw new Exception(); }
    /// <summary>
    /// Overload with a generic parameters
    /// </summary>
    public bool Foo(out TItem item, long param1) { throw new Exception();}
}

var genericMI = typeof(MyClass<>).GetMethod(
    "Foo",
    new[] 
    { 
         typeof(MyClass<>).GetGenericArguments().First().MakeByRefType(), 
         typeof(long)
    });

For non-generic you can use Type.MakeGenericMethodParameter(0).MakeByRefType() (actually it will work with MyClass<T> and override of generic parameter in Foo<T> too):

class MyClass
{
    /// <summary>
    /// The first version
    /// </summary>
    public int Foo(long param1) { throw new Exception(); }
    /// <summary>
    /// Overload with a generic parameters
    /// </summary>
    public bool Foo<TItem>(out TItem item, long param1) { throw new Exception();}
}

var genericMI = typeof(MyClass).GetMethod(
        "Foo",
        new[] 
        {
            Type.MakeGenericMethodParameter(0).MakeByRefType(),
            typeof(long)
        });


来源:https://stackoverflow.com/questions/61879161/c-sharp-how-to-use-getmethod-to-find-a-specific-overload-with-a-generic-out-para

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!