特性
https://blog.csdn.net/weixin_45136016/article/details/139151930
- 概念: 是一种
声明性标签
- 作用: 传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息
- 语法: 在声明时以
Attribute结尾,在使用时可省去Attribute
[attribute(positional_parameters, name_parameter = value, …)]
element
- positional_parameters: 表示特性必须具备的信息。
- name_paramete:表示特性可选的信息。
- element // 目标
// 声明时以"Attribute"结尾
[TypeConverterAttribute(typeof(NameToHumanTypeConverter))]
public class Human
{
......
}
public class NameToHumanTypeConverter : TypeConverter
{
......
}
// 声明特性: 该'特性'有一个属性和两个字段,无参和有参的构造方法
public class TestAttribute : Attribute
{
public int Parm { get; set; }
private int id;
private string name;
public TestAttribute()
{
}
public TestAttribute(int id, string name)
{
this.id = id;
this.name = name;
}
}
// 特性使用
[Test] // 使用无参构造函数
public class TestClass1
{
}
[Test(Parm = 123)] // 使用无参构造函数 + 指定参数
public class TestClass2
{
}
[Test(1, "test")] // 使用有参构造函数
public class TestClass3
{
}