.net使用自定义类属性实例
一般来说,在.net中可以使用Type.GetCustomAttributes获取类上的自定义属性,可以使用PropertyInfo.GetCustomAttributes获取属性信息上的自定义属性。
下面以定义一个简单数据库表的映射实体类来说明相关的使用方法,基于自定义类属性和自定义类中的属性的自定义属性,可以方便的进行类标记和类中属性的标记
创建一个类的自定义属性,用于标识数据库中的表名称,需要继承自Attribute类:
[AttributeUsage(AttributeTargets.Class,Inherited=false,AllowMultiple=false)]
publicsealedclassTableAttribute:Attribute
{
privatereadonlystring_TableName="";
publicTableAttribute(stringtableName)
{
this._TableName=tableName;
}
publicstringTableName
{
get{returnthis._TableName;}
}
}
创建一个属性的自定义属性,用于标识数据库表中字段的名称,需要继承自Attribute类:
[AttributeUsage(AttributeTargets.Property,Inherited=false,AllowMultiple=false)]
publicclassFieldAttribute:Attribute
{
privatereadonlystring_FieldName=""; ///数据库的字段名称
privateSystem.Data.DbType_Type=System.Data.DbType.String; ///数据库的字段类型
publicFieldAttribute(stringfieldName)
{
this._FieldName=fieldName;
}
publicFieldAttribute(stringfieldName,System.Data.DbTypetype)
{
this._FieldName=fieldName;
this._Type=type;
}
publicstringFieldName
{
get{returnthis._FieldName;}
}
publicSystem.Data.DbTypeType
{
get{returnthis._Type;}
}
}创建一个数据实体基类:
publicclassBaseEntity
{
publicBaseEntity()
{
}
///<summary>
///获取表名称
///</summary>
///<returns></returns>
publicstringGetTableName()
{
Typetype=this.GetType();
object[]objs=type.GetCustomAttributes(typeof(TableAttribute),true);
if(objs.Length<=0)
{
thrownewException("实体类没有标识TableAttribute属性");
}
else
{
objectobj=objs[0];
TableAttributeta=(TableAttribute)obj;
returnta.TableName; //获取表名称
}
}
///<summary>
///获取数据实体类上的FieldAttribute
///</summary>
///<paramname="propertyName"></param>
///<returns></returns>
publicFieldAttributeGetFieldAttribute(stringpropertyName)
{
PropertyInfofield=this.GetType().GetProperty(propertyName);
if(field==null)
{
thrownewException("属性名"+propertyName+"不存在");
}
object[]objs=field.GetCustomAttributes(typeof(FieldAttribute),true);
if(objs.Length<=0)
{
thrownewException("类体属性名"+propertyName+"没有标识FieldAttribute属性");
}
else
{
objectobj=objs[0];
FieldAttributefieldAttribute=(FieldAttribute)obj;
fieldAttribute.FieldValue=field.GetValue(this,null);
returnfieldAttribute;
}
}
}创建数据实体:
[Table("Wincms_Dictionary")] ///映射到数据库的Wincms_Dictionary表
publicclassWincms_Dictionary:BaseEntity
{
privateint_DictionaryId;
publicWincms_Dictionary()
{
}
[Field("DictionaryId",DbType.Int32)] ///映射到数据库的Wincms_Dictionary表中的字段
publicintDictionaryId
{
get{returnthis._DictionaryId;}
set
{
this._DictionaryId=value;
}
}
}
///基于实体类获取实体对应的表名称和字段名称
publicclassTest
{
publicstaticvoidmain(string[]args)
{
Wincms_Dictionarydict=newWincms_Dictionary();
Console.WriteLine("表名称:"+GetTableName(dict));
Console.WriteLine("字段名称:"+GetFieldName(dict,"DictionaryId"));
Console.Read();
}
///获取实体表名称
public staticstringGetTableName(BaseEntityentity)
{
returnentity.GetTableName();
}
///获取实体字段名称
publicstaticstringGetFieldName(BaseEntityentity,stringpropertyName)
{
FieldAttributefieldAttribute=entity.GetFieldAttribute(propertyName);
returnfieldAttribute.FieldName;
}
}输出结果为:
表名称:Wincms_Dictionary 字段名称:DictionaryId
希望本文所述对大家的.net程序设计有所帮助。