C#读取属性
示例
方法GetCustomAttributes返回应用于成员的自定义属性的数组。检索此数组后,您可以搜索一个或多个特定属性。
var attribute = typeof(MyClass).GetCustomAttributes().OfType<MyCustomAttribute>().Single();
或遍历他们
foreach(var attribute in typeof(MyClass).GetCustomAttributes()) {
Console.WriteLine(attribute.GetType());
}GetCustomAttribute扩展方法从中System.Reflection.CustomAttributeExtensions检索指定类型的自定义属性,它可以应用于anyMemberInfo。
var attribute = (MyCustomAttribute) typeof(MyClass).GetCustomAttribute(typeof(MyCustomAttribute));
GetCustomAttribute还具有通用签名以指定要搜索的属性的类型。
var attribute = typeof(MyClass).GetCustomAttribute<MyCustomAttribute>();
布尔参数inherit可以传递给这两种方法。如果将此值设置为trueelement的祖先,则也将进行检查。
热门推荐
1 泰迪狗狗如何训练
3 狗狗训练图
5 狗狗训练方法
6 训练狗狗不咬人
8 敏捷训练狗狗
10 训练狗狗吃饭
11 泰迪狗狗训练
12 狗狗从多大开始训练
13 狗狗训练学校费用
14 怎么去训练狗狗
15 狗狗训练狗狗
16 狗狗训练教程
17 训练狗狗装死
18 泰迪狗狗的训练方法