TypeDescriptor不会从继承的接口返回成员 [英] TypeDescriptor doesn't return members from inherited interfaces

查看:134
本文介绍了TypeDescriptor不会从继承的接口返回成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,TypeDescriptor不会从继承的接口返回的成员,这是它应该如何合作?或者是一个错误吗?



  [的TestFixture] 
公共类DescriptorTests
{
[测试]
公共无效测试()
{
//数= 1
Assert.AreEqual(2,TypeDescriptor.GetProperties(typeof运算(的IFoo))计数。);
//这是要失败的,该ID是不会被退回
}

公共接口IEntity
{
INT标识{搞定;组; }
}

公共接口的IFoo:IEntity
{
字符串名称{;组; }
}
}


解决方案

这是不是一个错误。从 ECMA CLI规范




11年9月8日的接口类型派生



接口类型可以要求一个的
实施或多种其它
的接口。用于实现一个接口类型应

支持任何类型还实施由
接口指定的任何
所需的接口支持。这是
对象类型不同的继承有两种方式:




  • 对象类型形成一个单一的继承树;接口类型做

  • 对象类型继承规定了如何实现继承。
    所需的接口不这样做,因为
    接口,不定义
    实施。必需的接口
    指定一个
    实施对象类型应
    支持的其他合同。



要亮点上次不同,
考虑的一个接口,的IFoo ,即
有一个单一的方法。一个接口,
伊巴尔,它提炼出来的,是
要求的任何对象类型
支持伊巴尔还支持的IFoo
它没有说什么哪
方法伊巴尔本身也会有。



< STRONG> 8.10成员继承



唯一对象类型可以继承
实现的,因此,只有对象
类可以继承成员(见
§8.9.8)。虽然接口类型可以从其他的接口类型,
衍生
,他们只是继承的要求,以
实现方法合同,从来没有
字段或方法实现。




修改...



如果你想获得的接口的特性,包括其祖先,那么你可以做这样的事情:

  VAR性能= typeof运算(的IFoo) 
.GetProperties()
.Union(typeof运算(的IFoo)
.GetInterfaces()
.SelectMany(T => t.GetProperties()));


my problem is that TypeDescriptor doesn't return members from inherited interfaces, is this how it is supposed to be working ? or is it a bug ?

 [TestFixture]
    public class DescriptorTests
    {
        [Test]
        public void Test()
        {
                                                                    // count = 1 
            Assert.AreEqual(2, TypeDescriptor.GetProperties(typeof(IFoo)).Count);
     // it is going to fail, the Id is not going to be returned
        }

        public interface IEntity
        {
            int Id { get; set; }
        }

        public interface IFoo : IEntity
        {
            string Name { get; set; }
        }
    }

解决方案

This isn't a bug. From the ECMA CLI specification:

8.9.11 Interface type derivation

Interface types can require the implementation of one or more other interfaces. Any type that implements support for an interface type shall also implement support for any required interfaces specified by that interface. This is different from object type inheritance in two ways:

  • Object types form a single inheritance tree; interface types do not.
  • Object type inheritance specifies how implementations are inherited; required interfaces do not, since interfaces do not define implementation. Required interfaces specify additional contracts that an implementing object type shall support.

To highlight the last difference, consider an interface, IFoo, that has a single method. An interface, IBar, which derives from it, is requiring that any object type that supports IBar also support IFoo. It does not say anything about which methods IBar itself will have.

8.10 Member inheritance

Only object types can inherit implementations, hence only object types can inherit members (see §8.9.8). While interface types can be derived from other interface types, they only "inherit" the requirement to implement method contracts, never fields or method implementations.

Edit...

If you want to get the properties of an interface, including those of its ancestors, then you could do something like this:

var properties = typeof(IFoo)
                     .GetProperties()
                     .Union(typeof(IFoo)
                                .GetInterfaces()
                                .SelectMany(t => t.GetProperties()));

这篇关于TypeDescriptor不会从继承的接口返回成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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