.net使用自定义类属性实例
程序员文章站
2024-02-20 22:16:53
一般来说,在.net中可以使用type.getcustomattributes获取类上的自定义属性,可以使用propertyinfo.getcustomattributes...
一般来说,在.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; }
}
}
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 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;
}
}
}
{
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;
}
}
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
字段名称:dictionaryid
希望本文所述对大家的.net程序设计有所帮助。