【问题标题】:Retrieving a Type's leaf interfaces检索类型的叶接口
【发布时间】:2010-11-30 10:15:01
【问题描述】:

System.Type 类提供了一个GetInterfaces() 方法,该方法“获取当前类型实现或继承的所有接口”。 问题是“GetInterfaces 方法不会以特定顺序返回接口,例如字母顺序或声明顺序。您的代码不能依赖于返回接口的顺序,因为该顺序会有所不同”。 然而,在我的情况下,我只需要隔离和公开(通过 WCF)接口层次结构的叶接口,即不被该层次结构中的其他接口继承的接口。例如,考虑以下层次结构

interface IA { }
interface IB : IA { }
interface IC : IB { }
interface ID : IB { }
interface IE : IA { }
class Foo : IC, IE {}

Foo 的叶子接口是 IC 和 IE,而 GetInterfaces() 将返回所有 5 个接口 (IA..IE)。 还提供了FindInterfaces() 方法,允许您使用您选择的谓词过滤上述接口。

我目前的实现如下。它是 O(n^2),其中 n 是该类型实现的接口数。我想知道是否有更优雅和/或更有效的方法。

    private Type[] GetLeafInterfaces(Type type)
    {
        return type.FindInterfaces((candidateIfc, allIfcs) =>
        {
            foreach (Type ifc in (Type[])allIfcs)
            {
                if (candidateIfc != ifc && candidateIfc.IsAssignableFrom(ifc))
                    return false;    
            }
            return true;
        }
        ,type.GetInterfaces());
    }

提前致谢

【问题讨论】:

    标签: c# reflection interface hierarchy


    【解决方案1】:

    我认为您不会找到一个简单的解决方案。您的问题是,最终,Foo 实际上实现了所有接口,而不管它们的内部继承层次结构如何。如果您使用 Ildasm 或其他类似工具检查 Foo,这将变得很明显。考虑以下代码:

    interface IFirst { }
    interface ISecond : IFirst { }
    class Concrete : ISecond { }
    

    生成的 IL 代码(从 Ildasm 转储):

    .class private auto ansi beforefieldinit MyNamespace.Concrete
           extends [mscorlib]System.Object
           implements MyNamespace.ISecond,
                      MyNamespace.IFirst
    {
      .method public hidebysig specialname rtspecialname 
              instance void  .ctor() cil managed
      {
        // Code size       7 (0x7)
        .maxstack  8
        IL_0000:  ldarg.0
        IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
        IL_0006:  ret
      } // end of method Concrete::.ctor
    
    } // end of class MyNamespace.Concrete
    

    如你所见,在这个层面上Concrete和两个接口之间的关系没有区别。

    【讨论】:

    • 谢谢!也许我的 GetLeafInterfaces() 函数将来可能对某人有用,那么
    猜你喜欢
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多