由于某种原因,我没有得到这个。 (下面的示例模式)如果我写:如何在获取属性时忽略继承链?
var property = typeof(sedan).GetProperty("TurningRadius");
Attribute.GetCustomAttributes(property,typeof(MyAttribute), false)
的通话将(2)表明,尽管我不希望搜索的继承链返回MyAttribute。有谁知道什么样的代码我可以写,这样调用
MagicAttributeSearcher(typeof(Sedan).GetProperty("TurningRadius"))
返回任何一边拨打
MagicAttributeSearcher(typeof(Vehicle).GetProperty("TurningRadius"))
回报MyAttribute(1)?
实例型号:
public class Sedan : Car
{
// ...
}
public class Car : Vehicle
{
[MyAttribute(2)]
public override int TurningRadius { get; set; }
}
public abstract class Vehicle
{
[MyAttribute(1)]
public virtual int TurningRadius { get; set; }
}
我不认为这个工程。该程序在测试轿车时找到了一个属性。这应该已经返回null。当你跑它时,它打印2行?如果是这样,它展示我遇到的问题。 – 2008-11-08 22:50:21
它打印了两行,但是四色轿车在冒号后有一个空白,即你想要的。我会编辑答案是绝对完整的。 – 2008-11-08 23:26:22