相关疑难解决方法(0)

C#反射,获取重载方法

我已经检查了一些关于反射和重载方法的其他帖子,但可以找到任何帮助。我发现的一篇文章是this one,但这并没有多大帮助。

我有以下两种方法:

1 | public void Delete<T>(T obj) where T : class { ... }
2 | public void Delete<T>(ICollection<T> obj) where T : class { ... }
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取方法 N°1。

我尝试了经典GetMethod("Delete")方法,但由于有两个具有此名称的方法,因此Ambiguous抛出了-Exception。我尝试使用附加参数指定方法架构,例如GetMethod("Delete", new [] { typeof(Object) })没有找到任何东西(返回空值)。

我想我不妨循环遍历所有方法并检查参数。

我写了以下方法...

    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();
    } …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection overloading generic-list

5
推荐指数
1
解决办法
1037
查看次数

C#反思:如何使用"对象"作为"类型参数"

我有这个域名:

public class GenClass<T> { }

public class Type1 { }

public class Type2 { }

public class GenType1 : GenClass<Type1> { }

public class GenType2 : GenClass<Type1> { }

public class GenType3 : GenClass<Type2> { }

public class GenType4 : GenClass<string> { } 

public class GenType5 : GenClass<int> { } 
Run Code Online (Sandbox Code Playgroud)

这个逻辑:

public class SomeClass {
    public void Add<T>(GenClass<T> t) { }
}
Run Code Online (Sandbox Code Playgroud)

而这个消费者:

public class Consumer {
    public void Method() {
        var some = new SomeClass();
        some.Add(new GenType1());
        some.Add(new …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection constructor type-parameter

1
推荐指数
1
解决办法
207
查看次数