的所有屬性就拿下面的示例代碼:得到一個被覆蓋的屬性
public class TestMultipleAttributesAttribute : AttributeWithPriority
{
public string HelpMessage { get; set; }
}
public class Student
{
[TestMultipleAttributes(HelpMessage = "Student")]
public virtual string Name { get; set; }
}
public class SpecificStudent : Student
{
[TestMultipleAttributes(Priority = 100, HelpMessage = "SpecificStudent")]
public override string Name { get; set; }
}
有什麼辦法通過反射我怎樣才能既TestMultipleAttributes
的兩個實例對同一被覆蓋的財產?
我嘗試下面的代碼:該來的一點是找到property.DeclaringType
[Test]
public void testMultipleAttribs()
{
var property = typeof(SpecificStudent).GetProperties().FirstOrDefault(x => x.Name == "Name");
var attribList = property.Attributes; //returns none
var customAttribs = property.CustomAttributes.ToList(); //returns 1
var customAttribs2 = property.GetCustomAttributes(inherit: true);// returns 1
int k = 5;
}
一種解決方案,並重復該過程,並獲得屬性吧。然而,我不覺得它是一個很好的方法來做到這一點,並想知道是否有更好的方法。
請,不包括有關在問題的標題,除非它是沒有意義的使用的語言信息沒有它。標籤用於此目的。 –