Ale*_*nor 11 .net c# reflection interface .net-4.0
我试图通过反射对类的接口执行查询,但是方法Type.GetInterfaces()也返回所有继承的接口.
等等
public class Test : ITest { }
public interface ITest : ITesting { }
Run Code Online (Sandbox Code Playgroud)
代码
typeof(Test).GetInterfaces();
Run Code Online (Sandbox Code Playgroud)
将返回一个Type[]包含both ITest和的地方ITesting,在我想要的地方ITest,是否有另一种允许你指定继承的方法?
谢谢,亚历克斯.
编辑:从下面的答案我收集了这个,
Type t;
t.GetInterfaces().Where(i => !t.GetInterfaces().Any(i2 => i2.GetInterfaces().Contains(i)));
Run Code Online (Sandbox Code Playgroud)
以上似乎有效,如果没有,请在评论中纠正我
你可以尝试这样的事情:
Type[] allInterfaces = typeof(Test).GetInterfaces();
var exceptInheritedInterfaces = allInterfaces.Except(
allInterfaces.SelectMany(t => t.GetInterfaces())
);
Run Code Online (Sandbox Code Playgroud)
我觉得有一种更优雅的方式,但我现在想不到它......
所以,如果你有这样的事情:
public interface A : B
{
}
public interface B : C
{
}
public interface C
{
}
public interface D
{
}
public class MyType : A, D
{
}
Run Code Online (Sandbox Code Playgroud)
代码将返回A和D.
| 归档时间: |
|
| 查看次数: |
1761 次 |
| 最近记录: |