简体   繁体   English

C#反射,得到重载方法

[英]C# reflection, get overloaded method

I've already checked a few other posts regarding reflection and overloaded methods but could find any help. 我已经检查了一些关于反射和重载方法的其他帖子,但可以找到任何帮助。 One post I found was this one , but that didn't help a lot. 我发现的一个帖子就是这个帖子,但这并没有多大帮助。

I have the following two methods: 我有以下两种方法:

1 | public void Delete<T>(T obj) where T : class { ... }
2 | public void Delete<T>(ICollection<T> obj) where T : class { ... }

I'm trying to get method N°1. 我正试图获得方法N°1。

I tried the classic GetMethod("Delete") approach, but since there are two methods with this name a Ambiguous -Exception was thrown. 我尝试了经典的GetMethod("Delete")方法,但由于这个名称有两个方法,因此引发了一个Ambiguous -Exception。 I tried specifying the method schema with an additional parameter like GetMethod("Delete", new [] { typeof(Object) }) which didn't find anything (null returned). 我尝试使用其他参数来指定方法模式,例如GetMethod("Delete", new [] { typeof(Object) }) ,它没有找到任何内容(null返回)。

I figgured I might as well just loop through all methods and check for the parameters. 我想我也可以循环遍历所有方法并检查参数。

I wrote the following method... 我写了以下方法......

    public static IEnumerable<MethodInfo> GetMethods(this Type type, String name, Type schemaExclude)
    {
        IEnumerable<MethodInfo> m = type.GetRuntimeMethods().Where(x => x.Name.Equals(name));
        return (from r in m let p = r.GetParameters() where !p.Any(o => schemaExclude.IsAssignableFrom(o.ParameterType)) select r).ToList();
    }

... which returns the methods which do not contain a parameter with type schemaExclude . ...返回不包含类型为schemaExclude的参数的方法。

I called it like this GetMethods("Delete", typeof(ICollection)) which didn't work as expected. 我称之为GetMethods("Delete", typeof(ICollection)) ,它没有按预期工作。

Apparently ..ICollection'1[T] is not assignable to ICollection . 显然..ICollection'1[T]不能分配给ICollection Neither is it to IEnumerable , IEnumerable<> and ICollection<> . 也不是IEnumerableIEnumerable<>ICollection<> I, again, tried it with typeof(Object) which did work but did return both methods (like its supposed to). 我再次尝试使用typeof(Object) ,它确实有效,但确实返回了两种方法(就像它应该的那样)。

What exactly am I missing? 我到底错过了什么?

You can look up the method by checking its generic parameter type, like this: 您可以通过检查其泛型参数类型来查找该方法,如下所示:

return type
    .GetRuntimeMethods()
    .Where(x => x.Name.Equals("Delete"))
    .Select(m => new {
         Method = m
    ,   Parameters = m.GetParameters()
    })
    .FirstOrDefault(p =>
        p.Parameters.Length == 1
    &&  p.Parameters[0].ParameterType.IsGenericType
    &&  p.Parameters[0].ParameterType.GetGenericTypeDefinition() == typeof(ICollection<>)
    )?.Method;

The above filters methods that match the criteria below: 以上筛选符合以下条件的方法:

  • Called 'Delete', 被称为“删除”,
  • With a single parameter, 使用单个参数,
  • With the parameter being a generic type, 参数是泛型类型,
  • With the generic parameter type constructed from ICollection<> 使用从ICollection<>构造的泛型参数类型

Demo. 演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM