我創建了一個自定義屬性&在我的課程的屬性(不是字段)中使用它。.NET MemberInfo.GetCustomAttributes不能用於類的屬性!
當我打電話FormatterServices.GetSerializableMembers,它確實給我的所有屬性
但是當我嘗試讀取使用MemberInfo.GetCustomAttributes屬性,它不給我任何價值。
當我嘗試實現相同的使用object.GetType()。GetProperties()。GetCustomAttributes,它完美的作品。
任何想法爲什麼它沒有提供MemberInfo中的信息?
[AttributeUsage(AttributeTargets.Property)]
public class MyAttribute : Attribute { }
//DOES NOT WORK
MemberInfo[] members = FormatterServices.GetSerializableMembers(recordObject.GetType());
object[] attributes = members[0].GetCustomAttributes(typeof(MyAttribute), false)
//WORKS
PropertyInfo[] properties = recordObject.GetType().GetProperties();
object[] attributes = properties[0].GetCustomAttributes(typeof(MyAttribute), false);
您能否構建一個簡短但完整的例子來演示問題並將其添加到您的問題中? – 2011-01-20 13:28:55