特性(Attribute)
特性(Attribute)
常用的系统特性
Serializable:常用于标记类,表示可被序列化
Nonserialized :不允许序列化,在被标注为Serializable序列化的类中,某字段前加Nonserialized,表示该字段不允许序列化。
Obsolete: 标记方法,被标记的方法在调用时会产生警告或报错。
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)][Serializable]public class MyAttribute{[NonSerialized] //仅对字段有效public int MyProperty;[Obsolete("这个方法不要用了,我设置了报错",true)]public void Show(){Console.WriteLine("test");}}
AttributeUsage:也是一个特性,是用来修饰特性的特性,一般标记在特性类上
AttributeTargets指定当前特性只能标记在某个地方(属性、方法、字段等地方);AttributeTargets.All表示可标记在任意地方
AllowMultiple:是否可以重复标记
Inherited:是否可以继承特性
自定义特性
1.每使用一个特性标记一次都相当于实例化了一个特性对象
2.可以通过反射获取被标记的特性。
namespace MyAttribute
{[User(100)][User("我是类上的特性")]public class UserInfor{[User(200)][User("我是属性上的特性")]public int MyProperty { get; set; }[User(300)][User("我是方法上的特性")]public void Show(){}}[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]public class UserAttribute: Attribute{public int Id = 0;public string Name { get; set; } = "我是空的";public string PassWord { get; set; }public UserAttribute(){}public UserAttribute( int Id){this.Id = Id;}public UserAttribute(string Name){this.Name = Name;}public string Show(){return this.PassWord;}}
}
namespace MyAttribute
{internal class Program{static void Main(string[] args){UserInfor userInfor = new UserInfor();Type type = userInfor.GetType();if (type.IsDefined(typeof(UserAttribute),true)) //判断type这个类上是否有UserAttribute的标记{//GetCustomAttributes:获取标记在类外层的特性 true表示继承的也算foreach (UserAttribute item in type.GetCustomAttributes(true)){Console.WriteLine($"{item.Id}");Console.WriteLine($"{item.Name}");}}foreach (var item in type.GetProperties()){//判断item这个属性上是否有UserAttribute的标记if (item.IsDefined(typeof(UserAttribute), true)){//获取标记在属性上的所有特性 true表示继承的也算foreach (UserAttribute userAttribute in item.GetCustomAttributes(true)){Console.WriteLine($"{userAttribute.Id}");Console.WriteLine($"{userAttribute.Name}");}}}foreach (var item in type.GetMethods()){//判断item这个方法上是否有UserAttribute的标记if (item.IsDefined(typeof(UserAttribute), true)){//获取标记在属性上的所有特性 true表示继承的也算foreach (UserAttribute userAttribute in item.GetCustomAttributes(true)){Console.WriteLine($"{userAttribute.Id}");Console.WriteLine($"{userAttribute.Name}");}}}Console.ReadLine();}}
}
特性(Attribute)
特性(Attribute)
常用的系统特性
Serializable:常用于标记类,表示可被序列化
Nonserialized :不允许序列化,在被标注为Serializable序列化的类中,某字段前加Nonserialized,表示该字段不允许序列化。
Obsolete: 标记方法,被标记的方法在调用时会产生警告或报错。
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)][Serializable]public class MyAttribute{[NonSerialized] //仅对字段有效public int MyProperty;[Obsolete("这个方法不要用了,我设置了报错",true)]public void Show(){Console.WriteLine("test");}}
AttributeUsage:也是一个特性,是用来修饰特性的特性,一般标记在特性类上
AttributeTargets指定当前特性只能标记在某个地方(属性、方法、字段等地方);AttributeTargets.All表示可标记在任意地方
AllowMultiple:是否可以重复标记
Inherited:是否可以继承特性
自定义特性
1.每使用一个特性标记一次都相当于实例化了一个特性对象
2.可以通过反射获取被标记的特性。
namespace MyAttribute
{[User(100)][User("我是类上的特性")]public class UserInfor{[User(200)][User("我是属性上的特性")]public int MyProperty { get; set; }[User(300)][User("我是方法上的特性")]public void Show(){}}[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]public class UserAttribute: Attribute{public int Id = 0;public string Name { get; set; } = "我是空的";public string PassWord { get; set; }public UserAttribute(){}public UserAttribute( int Id){this.Id = Id;}public UserAttribute(string Name){this.Name = Name;}public string Show(){return this.PassWord;}}
}
namespace MyAttribute
{internal class Program{static void Main(string[] args){UserInfor userInfor = new UserInfor();Type type = userInfor.GetType();if (type.IsDefined(typeof(UserAttribute),true)) //判断type这个类上是否有UserAttribute的标记{//GetCustomAttributes:获取标记在类外层的特性 true表示继承的也算foreach (UserAttribute item in type.GetCustomAttributes(true)){Console.WriteLine($"{item.Id}");Console.WriteLine($"{item.Name}");}}foreach (var item in type.GetProperties()){//判断item这个属性上是否有UserAttribute的标记if (item.IsDefined(typeof(UserAttribute), true)){//获取标记在属性上的所有特性 true表示继承的也算foreach (UserAttribute userAttribute in item.GetCustomAttributes(true)){Console.WriteLine($"{userAttribute.Id}");Console.WriteLine($"{userAttribute.Name}");}}}foreach (var item in type.GetMethods()){//判断item这个方法上是否有UserAttribute的标记if (item.IsDefined(typeof(UserAttribute), true)){//获取标记在属性上的所有特性 true表示继承的也算foreach (UserAttribute userAttribute in item.GetCustomAttributes(true)){Console.WriteLine($"{userAttribute.Id}");Console.WriteLine($"{userAttribute.Name}");}}}Console.ReadLine();}}
}