读一个属性
方法 GetCustomAttributes 返回应用于成员的自定义属性数组。检索此数组后,你可以搜索一个或多个特定属性。
var attribute = typeof(MyClass).GetCustomAttributes().OfType<MyCustomAttribute>().Single();
或者遍历它们
foreach(var attribute in typeof(MyClass).GetCustomAttributes()) {
    Console.WriteLine(attribute.GetType());
}
System.Reflection.CustomAttributeExtensions 的 GetCustomAttribute 扩展方法检索指定类型的自定义属性,它可以应用于任何 MemberInfo。
var attribute = (MyCustomAttribute) typeof(MyClass).GetCustomAttribute(typeof(MyCustomAttribute));
GetCustomAttribute 还具有通用签名,用于指定要搜索的属性类型。
var attribute = typeof(MyClass).GetCustomAttribute<MyCustomAttribute>();
布尔参数 inherit 可以传递给这两个方法。如果将此值设置为 true,则元素的祖先也将被检查。