• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    .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

    希望本文所述对大家的.net程序设计有所帮助。

    您可能感兴趣的文章:
    • 浅谈python类属性的访问、设置和删除方法
    • PHP中类属性与类静态变量的访问方法示例
    • MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题
    • Python类属性与实例属性用法分析
    • PowerShell中调用.NET对象的静态方法、静态属性和类方法、类属性例子
    • 从零学Python之引用和类属性的初步理解
    • JavaScript类属性的访问方式详解
    • 在Struts2中如何将父类属性序列化为JSON格式的解决方法
    • python 基础学习第二弹 类属性和实例属性
    • Python类属性的延迟计算
    上一篇:.NET实现在网页中预览Office文件的3个方法
    下一篇:ASP.NET中常用输出JS脚本的类实例
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    .net使用自定义类属性实例 .net,使用,自定义,类,属性,