足球游戏_中国足彩网¥体育资讯$

.net使用自定义类属性实例
来源:易贤网 阅读:635 次 日期:2014-10-16 10:31:21
温馨提示:易贤网小编为您整理了“.net使用自定义类属性实例”,方便广大网友查阅!

一般来说,在.net中可以使用Type.GetCustomAttributes获取类上的自定义属性,可以使用PropertyInfo.GetCustomAttributes获取属性信息上的自定义属性。

下面以定义一个简单数据库表的映射实体类来说明相关的使用方法,基于自定义类属性和自定义类中的属性的自定义属性,可以方便的进行类标记和类中属性的标记

创建一个类的自定义属性,用于标识数据库中的表名称,需要继承自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; }

}

}

创建一个属性的自定义属性,用于标识数据库表中字段的名称,需要继承自Attribute类:

代码如下:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]

public class FieldAttribute : Attribute

{

private readonly string _FieldName = ""; ///数据库的字段名称

private System.Data.DbType _Type = System.Data.DbType.String; ///数据库的字段类型

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;}

}

}

创建一个数据实体基类:

代码如下:

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("实体类没有标识TableAttribute属性");

}

else

{

object obj = objs[0];

TableAttribute ta = (TableAttribute)obj;

return ta.TableName; //获取表名称

}

}

/// <summary>

/// 获取数据实体类上的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 + "没有标识FieldAttribute属性");

}

else

{

object obj = objs[0];

FieldAttribute fieldAttribute=(FieldAttribute)obj;

fieldAttribute.FieldValue=field.GetValue(this,null);

return fieldAttribute;

}

}

}

创建数据实体:

代码如下:

[Table("Wincms_Dictionary")] ///映射到数据库的Wincms_Dictionary表

public class Wincms_Dictionary : BaseEntity

{

private int _DictionaryId;

public Wincms_Dictionary()

{

}

[Field("DictionaryId",DbType.Int32)] ///映射到数据库的Wincms_Dictionary表中的字段

public int DictionaryId

{

get { return this._DictionaryId; }

set

{

this._DictionaryId = value;

}

}

}

///基于实体类获取实体对应的表名称和字段名称

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;

}

}

输出结果为:

代码如下:

表名称:Wincms_Dictionary

字段名称:DictionaryId

中国足彩网信息请查看IT技术专栏

中国足彩网信息请查看网络编程
易贤网手机网站地址:.net使用自定义类属性实例
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标