那么,如果我有:
public class Sedan : Car
{
/// ...
}
public class Car : Vehicle, ITurn
{
[MyCustomAttribute(1)]
public int TurningRadius { get; set; }
}
public abstract class Vehicle : ITurn
{
[MyCustomAttribute(2)]
public int TurningRadius { get; set; }
}
public interface ITurn
{
[MyCustomAttribute(3)]
int TurningRadius { get; set; }
}
我该如何使用魔法来实现像这样的操作:
[Test]
public void Should_Use_Magic_To_Get_CustomAttributes_From_Ancestry()
{
var property = typeof(Sedan).GetProperty("TurningRadius");
var attributes = SomeMagic(property);
Assert.AreEqual(attributes.Count, 3);
}
请问需要翻译的内容是什么?
property.GetCustomAttributes(true);
并且
Attribute.GetCustomAttributes(property, true);
只返回1个属性。实例是使用MyCustomAttribute(1)构建的。这似乎没有按预期工作。