using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
[FieldName("UsersName")]
public string UsersName
{ get; set; }
[FieldName2("Test2", Comment = "This is a test code")] /*Comment 指定可选参数,编译器不会使用Comment来构造生成特性,而是在特性中寻找一个public 的属性进行赋值*/
public int Age
{
get;set;
}
static void Main(string[] args)
{
}
}
[AttributeUsage( /* 这个是只用于修饰Attribute的特性 */
AttributeTargets.Property, /* All Assembly Class Constuctor Delegate Enum Event Field GenericParameter Interface Method Parameter Property ReturnValue Struct
不同程序元素 可以使用 | assembly 和 module使用时 要在前面显示标记 */
AllowMultiple = true, /* 可以将同一个特性多次标记一个目标 [FieldName("asd")][FieldName("dsdsdsc")] public string Name{get;set;} */
Inherited = true /* 设置为 true ,继承 类或接口可以获得该特性*/
)]
internal class FieldNameAttribute : Attribute
{
string _name;
public FieldNameAttribute(string name)
{
this._name = name;
}
}
internal class FieldName2Attribute : Attribute
{
public string Comment
{ get; set; }
public string _name;
public FieldName2Attribute(string name)
{
this._name = name;
}
}
}