我创建了一个自定义属性&在我的课程的属性(不是字段)中使用它。.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