反映了一个多接口的所有特性,包括继承的呢? [英] Reflecting over all properties of an interface, including inherited ones?

查看:157
本文介绍了反映了一个多接口的所有特性,包括继承的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示接口的System.Type的实例,我希望得到那个接口上的所有属性的列表 - 包括那些从基接口继承。我基本上是想从我得到的类接口相同的行为。

I have an instance of System.Type that represents an interface, and I want to get a list of all the properties on that interface -- including those inherited from base interfaces. I basically want the same behavior from interfaces that I get for classes.

例如,鉴于这一层次:

public interface IBase {
    public string BaseProperty { get; }
}
public interface ISub : IBase {
    public string SubProperty { get; }
}
public class Base : IBase {
    public string BaseProperty { get { return "Base"; } }
}
public class Sub : Base, ISub {
    public string SubProperty { get { return "Sub"; } }
}

如果我呼吁类的GetProperties - typeof运算(子).GetProperties() - 然后我得到两个BaseProperty和子属性。我想要做同样的事情与接口,但是当我尝试 - typeof运算(ISUB).GetProperties() - 所有回来的子属性

If I call GetProperties on the class -- typeof(Sub).GetProperties() -- then I get both BaseProperty and SubProperty. I want to do the same thing with the interface, but when I try it -- typeof(ISub).GetProperties() -- all that comes back is SubProperty.

我试图传递 BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy 来的GetProperties,因为我FlattenHierarchy的理解是,它应该包括从基类成员,但行为是完全一样的。

I tried passing BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy to GetProperties, since my understanding of FlattenHierarchy is that it's supposed to include members from base classes, but the behavior was exactly the same.

我想我可以遍历 Type.GetInterfaces(),并呼吁每个人的GetProperties,但后来我会依靠的GetProperties接口上的从不回归基本属性(因为如果做过,我会得到副本)。我宁愿不依赖于这种行为,而至少看到它记录

I suppose I could iterate Type.GetInterfaces() and call GetProperties on each one, but then I would be relying on GetProperties on an interface to never return base properties (since if it ever did, I'd get duplicates). I'd rather not rely on this behavior without at least seeing it documented.

如何之一:


  • 获取一个接口上的所有属性,包括那些从基接口的列表?或

  • 在至少有信心,我所看到的记载,我可以依靠的行为,这样我就可以解决它?

推荐答案

各种各样的答案是在的注释的到的 3.5专用.NET framework版本的MSDN上的的GetProperties(的BindingFlags的BindingFlags页面)

An answer of sorts is to be found in an annotation to the .NET framework version 3.5-specific MSDN page on GetProperties(BindingFlags bindingFlags) :

传BindingFlags.FlattenHierarchy
到Type.GetXXX之一方法,
如Type.GetMembers,不会当您查询的接口
类型本身
返回继承接口成员

Passing BindingFlags.FlattenHierarchy to one of the Type.GetXXX methods, such as Type.GetMembers, will not return inherited interface members when you are querying on an interface type itself.

[...]

要获得继承的成员,则需要
查询每个实现的接口
为它的成员。

To get the inherited members, you need to query each implemented interface for its members.

的示例代码也包括在内。此评论被张贴一个Microsoftie,所以我会说你可以信任它。

Example code is also included. This comment was posted by a Microsoftie, so I would say you can trust it.

这篇关于反映了一个多接口的所有特性,包括继承的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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