태그 속성

5277 단어 attribute
한 클래스 에 탭 을 놓 으 면 특정한 규칙 을 표시 할 수 있 습 니 다. 예 를 들 어 특정한 대상 의 특정한 속성 이 제 이 슨 화 되 고 싶 지 않 으 면 우 리 는 그 머리 에 탭 을 놓 으 면 됩 니 다. 또는 ORM 을 할 때 특정한 Class 에 대응 하 는 table 이름 등 을 지정 합 니 다.
 
마지막 탭 은 반 사 를 통 해 호출 됩 니 다. 하 나 는 마이크로소프트 의 Attribute 류 를 계승 하면 탭 으로 사용 할 수 있 습 니 다.
 
[AttributeUsage(AttributeTargets.All)]//  Attribute     ,     class    

public class Column : Attribute

{

    public Column(string str)

    {

        this.str = str;

    }

    public string str { get; set; }

}

// 2 

[AttributeUsage(AttributeTargets.All)]

public class Table : Attribute

{
public string someData { get; set; } //
public Table(string str) { this.str = str; } public string str { get; set; } } //2 [Column("class")] [Table("table class", someData = "ggc" )] //someData="ggc" attribute ! public class Abc { public string x { get; set; } [Column("attr")] public Int32 y { get; set; } } protected void Page_Load(object sender, EventArgs e) { // attr Type type = typeof(Abc);
 bool isGot = Attribute.IsDefined(type, typeof(Column)); // Attribute Attribute[] attrs
= Attribute.GetCustomAttributes(type); // string xy = attrs.First().GetType().Name; // , . Column column = (Column)Attribute.GetCustomAttribute(type, typeof(Column)); // Column[] columns = (Column[])Attribute.GetCustomAttributes(type, typeof(Column)); column = columns[0]; string x = column.str; // // Abc a = new Abc(); PropertyInfo info = a.GetType().GetProperty("y"); Column attribute2 = (Column)Attribute.GetCustomAttribute(info, typeof(Column)); string xyz = attribute2.str; }

좋은 웹페이지 즐겨찾기