只获取直接接口而不是全部?
我有一个像下面这样的课程。 GetInterfaces() 说
如果当前Type代表一个类型 a 定义中的参数 泛型类型或泛型方法,这 方法搜索接口 约束和任何接口 从类或接口继承 限制条件。
我有可能得不到任何继承的接口吗?当我在 ABC 上使用 GetInterfaces 时,我只想查看 DEF,而不是 DEF 和 GHI。
interface DEF : GHI {...}
class ABC : DEF {...}
I have a class like the below. GetInterfaces() says
If the current Type represents a type
parameter in the definition of a
generic type or generic method, this
method searches the interface
constraints and any interfaces
inherited from class or interface
constraints.
Is it possible for me to not get any inherited interface? When i use GetInterfaces on ABC i only want to see DEF, not DEF and GHI.
interface DEF : GHI {...}
class ABC : DEF {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是来自 重复问题:
和用法:
This is a nice piece from a duplicate question:
And usage:
据我所知,所发布的解决方案均不适用于下面示例中的
typeof(C1)
和typeof(I3)
。我认为正确的答案是不可能使用开箱即用的框架调用来获取直接实现的接口。
As far as I can tell, none of the posted solutions work for
typeof(C1)
andtypeof(I3)
in the example below.I think the correct answer is that it is not possible to get the directly implemented interfaces using out-of-the-box framework calls.
对于接口继承层次来说,这怎么样?
How about this for interface inheritance heirachy?
同一个 MSDN 页面说
所以不,你不能跳过界面。
The same MSDN page says
So no, you can't skip interfaces.
此问题在此处和此处。无论如何,这是我的简洁代码,考虑了 @Ani 和 @Mikael 在此线程中提出的观点:
This question is duplicated both here and here. In any case, here's my concise code that considers the points raised on this thread by @Ani and @Mikael:
首先,您发布的 MSDN 片段与您的实际问题没有任何关系。例如,它会处理您拥有诸如
class Foo之类的泛型类型的情况。其中 T : IEnumerable
,然后尝试在类型参数T
上调用GetInterfaces
,例如通过typeof(Foo<>).GetGenericArguments ().Single().GetInterfaces().
其次,问题描述得有点不明确。请注意,当一个类实现一个接口时,它必须实现该接口“继承”的所有接口。它只是一个 C# 便利功能,允许您在类声明中省略继承的接口。在您的示例中,显式包含“继承的”
GHI
接口是完全合法的(没有什么不同):我假设您真正想做的是找到一个“覆盖”所有类型的已实现接口的“最小集合”接口。这会导致设置覆盖问题的稍微简化版本。
这是解决该问题的一种方法,无需任何尝试提高算法效率。这个想法是通过过滤掉那些已经由该类型实现的其他接口实现的接口来生成最小接口集。
(
编辑 - 这是执行上述操作的更好方法
:)
例如,对于
List
:请注意,此解决方案涵盖接口'仅层次结构(这似乎是您想要的),而不是它们与类的 class 层次结构的关系。特别是,它不关心接口在类的层次结构中首先实现的何处。
例如,假设我们有:
现在,如果您尝试使用我描述的解决方案来获取
Derived
的最小接口集,您将获得IBaz
以及IBar
。如果您不需要IBar
,则必须付出更多努力:消除基类实现的接口。最简单的方法是从最小接口集中删除由类的直接基类实现的那些接口,正如 @MikeEast 的答案中提到的。Firstly, the MSDN snippet you've posted doesn't have anything to do with your actual question. It deals with when you have, for example, a generic type such as
class Foo<T> where T : IEnumerable
, and you try callingGetInterfaces
on the type-parameterT
, for example throughtypeof(Foo<>).GetGenericArguments().Single().GetInterfaces().
Secondly, the problem is slightly ill-specified. Note that when a class implements an interface, it must implement all of the interfaces 'inherited' by that interface. It's simply a C# convenience feature that lets you omit the inherited interfaces in the class-declaration. In your example, it's perfectly legal (and no different) to explicitly include the 'inherited'
GHI
interface:I've assumed that what you really want to do is find a 'minimal set' of interfaces that 'covers' all of the type's implemented interfaces. This results in a slightly simplified version of the Set cover problem.
Here's one way to solve it, without any attempt whatsoever to be algorithmically efficient. The idea is to produce the minimal interface-set by filtering out those interfaces that are already implemented by other interfaces implemented by the type.
(
EDIT - Here's a better way of doing the above:
)
For example, for
List<int>
:Do note that this solution covers interface 'hierarchies' only (which is what you appear to want), not how they relate to the class's class hierarchy. In particular, it pays no attention to where in a class's hierarchy an interface was first implemented.
For example, let's say we have:
Now if you try using the solution I've described to get the minimal interface-set for
Derived
, you would getIBaz
as well asIBar
. If you don't wantIBar
, you would have to go to more effort: eliminate interfaces implemented by base-classes. The easiest way to do this would be to remove from the minimal interface-set those interfaces implemented by the class's immediate base-class, as is mentioned in @MikeEast's answer.