【发布时间】:2009-11-23 14:18:19
【问题描述】:
在对接口类型进行反射时,我只获取特定类型的成员,而不是继承的成员。
在这个过于简化的示例中,程序只打印“Name”,而不是“ItemNumber”,“Name”,正如我所期望的那样:
using System;
public interface IBasicItem
{
string ItemNumber { get; set; }
}
public interface IItem : IBasicItem
{
string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
var type = typeof (IItem);
foreach (var prop in type.GetProperties())
Console.WriteLine(prop.Name);
}
}
这背后的原理是什么?当我从基接口继承时,我是说我的接口的任何实现都必须实现继承的成员。换句话说,IItem is-a IBasicItem。那么为什么继承的成员不使用反射显示呢?
【问题讨论】:
标签: c# reflection