仅获得直接接口,而不是全部? [英] Get only direct interface instead of all?

查看:118
本文介绍了仅获得直接接口,而不是全部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的低一级别。 GetInterfaces()说:




如果当前Type表示在
泛型类型的定义类型
参数或一般的方法,这种
方法搜索界面
约束和任何接口
从类或接口
约束继承。




是否有可能对我来说,没有得到任何继承接口?当我用ABC GetInterfaces我只希望看到DEF,而不是DEF和GHI

 接口DEF:GHI {... } 
类ABC:DEF {...}


解决方案

首先,MSDN片段您发布没有任何与您的实际问题。它与当你有涉及,例如,泛型类型,如类Foo< T>其中T:IEnumerable的,你尝试调用 GetInterfaces 的类型参数 T ,例如,通过 typeof运算(富<>)。GetGenericArguments()单()GetInterfaces()



其次,问题是略微不良指定。请注意,当一个类实现一个接口,它必须实现的所有的由该接口的接口继承。它只是一个C#的便利功能,让你忽略继承的接口在类声明。在你的榜样,这是完全合法的(没有不同)到的明确的包括继承 GHI 接口:

  ABC类:DEF,GHI {...} 






我假设你真正想要做的是找到接口最小集说涵盖所有类型的实现的接口。这导致集合覆盖问题的略微简化版本。



下面是解决这个问题的一种方式,没有任何企图无论如何是算法效率。这个想法是产生最小接口设置过滤掉那些的的实施通过这些接口的其他的由类型实现的接口。

 输入型= ... 

VAR allInterfaces = type.GetInterfaces();
变种minimalInterfaces从ITYPE =在allInterfaces
,其中allInterfaces.Any!(T => t.GetInterfaces()
。载有(ITYPE))
选择ITYPE;




修改 - 在这里是做一个更好的办法上图:

  VAR minimalInterfaces = allInterfaces.Except 
(allInterfaces.SelectMany(T => t.GetInterfaces() ));



例如对于为列表< INT>

  allInterfaces:

System.Collections.Generic.IList`1 [System.Int32]
System.Collections.Generic.ICollection`1 [System.Int32]
System.Collections.Generic.IEnumerable`1 [ System.Int32]
System.Collections.IEnumerable
System.Collections.IList
System.Collections.ICollection

minimalInterfaces:

系统.Collections.Generic.IList`1 [System.Int32]
System.Collections.IList






请注意,这个解决方案涵盖的接口的'层次'只(这是你似乎什么想要的),而不是如何将它们与类的的的层次。特别是,它不关注的其中,在类的层次结构中的界面开始实施



例如,假设我们有:

 接口的IFoo {} 
接口伊巴尔:的IFoo {}
接口IBaz {}

级基地:伊巴尔{}
类派生:基地IBaz {}

现在,如果你尝试使用该解决方案我所描述得到最少的接口设置为派生,你会得到 IBaz 以及伊巴尔。如果你不想让伊巴尔,你就必须去更多的努力:消除基类实现的接口。要做到这一点,最简单的方法是由类的直接基类实现的最小接口设置这些接口删除,如被提及@ MikeEast的答案。


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 {...}

解决方案

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 calling GetInterfaces on the type-parameter T, for example through typeof(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:

class ABC : DEF, GHI {...}


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.

Type type = ...

var allInterfaces = type.GetInterfaces();    
var minimalInterfaces = from iType in allInterfaces 
                        where !allInterfaces.Any(t => t.GetInterfaces()
                                                       .Contains(iType))
                        select iType;

( EDIT - Here's a better way of doing the above:

var minimalInterfaces = allInterfaces.Except
                        (allInterfaces.SelectMany(t => t.GetInterfaces()));

)

For example, for List<int>:

allInterfaces: 

System.Collections.Generic.IList`1[System.Int32]
System.Collections.Generic.ICollection`1[System.Int32]
System.Collections.Generic.IEnumerable`1[System.Int32]
System.Collections.IEnumerable
System.Collections.IList
System.Collections.ICollection

minimalInterfaces:

System.Collections.Generic.IList`1[System.Int32]
System.Collections.IList


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:

interface IFoo { }
interface IBar : IFoo { }
interface IBaz { } 

class Base : IBar {  }
class Derived : Base, IBaz {  }

Now if you try using the solution I've described to get the minimal interface-set for Derived, you would get IBaz as well as IBar. If you don't want IBar, 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.

这篇关于仅获得直接接口,而不是全部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆