.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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值