国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

.net使用自定義類屬性

2019-11-17 02:50:37
字體:
供稿:網(wǎng)友
.net使用自定義類屬性

.net中可以使用Type.GetCustomAttributes獲取類上的自定義屬性,可以使用PRopertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。

下面以定義一個簡單數(shù)據(jù)庫表的映射實體類來說明相關(guān)的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進行類標(biāo)記和類中屬性的標(biāo)記

創(chuàng)建一個類的自定義屬性,用于標(biāo)識數(shù)據(jù)庫中的表名稱,需要繼承自Attribute類:

[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] public sealed class TableAttribute : Attribute { private readonly string _TableName = ""; public TableAttribute(string tableName) { this._TableName = tableName; } public string TableName { get { return this._TableName; } } }

創(chuàng)建一個屬性的自定義屬性,用于標(biāo)識數(shù)據(jù)庫表中字段的名稱,需要繼承自Attribute類

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)] public class FieldAttribute : Attribute { private readonly string _FieldName = ""; ///數(shù)據(jù)庫的字段名稱

private System.Data.DbType _Type = System.Data.DbType.String; ///數(shù)據(jù)庫的字段類型

public FieldAttribute(string fieldName)

{

this._FieldName=fieldName;

}

public FieldAttribute(string fieldName,System.Data.DbType type)

{

this._FieldName=fieldName;

this._Type=type;

}

public string FieldName { get { return this._FieldName; } }

public System.Data.DbType Type

{

get{return this._Type;}

}

}

創(chuàng)建一個數(shù)據(jù)實體基類:

public class BaseEntity{ public BaseEntity() { }

/// <summary> /// 獲取表名稱 /// </summary> /// <returns></returns> public string GetTableName() { Type type = this.GetType(); object[] objs = type.GetCustomAttributes(typeof(TableAttribute), true); if (objs.Length <= 0) { throw new Exception("實體類沒有標(biāo)識TableAttribute屬性"); } else { object obj = objs[0]; TableAttribute ta = (TableAttribute)obj; return ta.TableName; //獲取表名稱 } } /// <summary> /// 獲取數(shù)據(jù)實體類上的FieldAttribute /// </summary> /// <param name="propertyName"></param> /// <returns></returns> public FieldAttribute GetFieldAttribute(string propertyName) { PropertyInfo field = this.GetType().GetProperty(propertyName); if (field == null) { throw new Exception("屬性名" + propertyName + "不存在"); } object[] objs = field.GetCustomAttributes(typeof(FieldAttribute), true); if (objs.Length <= 0) { throw new Exception("類體屬性名" + propertyName + "沒有標(biāo)識FieldAttribute屬性"); } else { object obj = objs[0]; FieldAttribute fieldAttribute=(FieldAttribute)obj; fieldAttribute.FieldValue=field.GetValue(this,null); return fieldAttribute; } }

}

創(chuàng)建數(shù)據(jù)實體

[Table("Wincms_Dictionary")] ///映射到數(shù)據(jù)庫的Wincms_Dictionary表public class Wincms_Dictionary : BaseEntity{

private int _DictionaryId;

public Wincms_Dictionary()

{

}

[Field("DictionaryId",DbType.Int32)] ///映射到數(shù)據(jù)庫的Wincms_Dictionary表中的字段 public int DictionaryId { get { return this._DictionaryId; } set { this._DictionaryId = value; } }

}

///基于實體類獲取實體對應(yīng)的表名稱和字段名稱

public class Test

{

public static void main(string[] args)

{

Wincms_Dictionary dict=new Wincms_Dictionary();

Console.WriteLine("表名稱:"+GetTableName(dict));

Console.WriteLine("字段名稱:"+GetFieldName(dict,"DictionaryId"));

Console.Read();

}

///獲取實體表名稱

public static string GetTableName(BaseEntity entity)

{

return entity.GetTableName();

}

///獲取實體字段名稱

public static string GetFieldName(BaseEntity entity,string propertyName)

{

FieldAttribute fieldAttribute=entity.GetFieldAttribute(propertyName);

return fieldAttribute.FieldName;

}

}

輸出結(jié)果為:

表名稱:Wincms_Dictionary

字段名稱:DictionaryId


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 融水| 土默特左旗| 平凉市| 延吉市| 巴塘县| 札达县| 江安县| 拉孜县| 汉川市| 岑溪市| 石楼县| 滦南县| 介休市| 微山县| 罗源县| 乾安县| 清河县| 桐庐县| 东丰县| 泊头市| 泾阳县| 樟树市| 久治县| 岳阳市| 定边县| 威海市| 堆龙德庆县| 吉林市| 龙山县| 泽州县| 法库县| 广元市| 清新县| 当雄县| 千阳县| 哈巴河县| 平和县| 固安县| 木兰县| 大方县| 高青县|