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

.net使用自定义类属性实例
来源:易贤网 阅读:504 次 日期:2014-10-21 10:09:55
温馨提示:易贤网小编为您整理了“.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)版权所有:易贤网
云南网警报警专用图标