• System.Attribute 类的使用


    最近自己写了个ORM框架,里面用到的 属性和字段的特性装饰
    定义Attribute装饰类、实体类、字段结构体:
    View Code
        [AttributeUsage(AttributeTargets.Property)] //此属性只能用在Property上 
        public class CyAttribute : Attribute
        {
            private string _datatype = string.Empty;
            private int _length = 0;
    
            public CyAttribute(string datatype, int length)
            {
                _datatype = datatype;
                _length = length;
            }
    
            public string DataType
            {
                get { return _datatype; }
                set { _datatype = value; }
            }
            public int Length
            {
                get { return _length; }
                set { _length = value; }
            }
        }
        [AttributeUsage(AttributeTargets.Field)] //此属性只能用在Field上 
        public class CyAttributeField : Attribute
        {
            private string _datatype = string.Empty;
            private int _length = 0;
    
            public CyAttributeField(string datatype, int length)
            {
                _datatype = datatype;
                _length = length;
            }
    
            public string DataType
            {
                get { return _datatype; }
                set { _datatype = value; }
            }
            public int Length
            {
                get { return _length; }
                set { _length = value; }
            }
        } 
        [AttributeUsage(AttributeTargets.Field)] //此属性只能用在Field上 
        public class CyAttribute2 : Attribute
        {
            private string _op = string.Empty;
    
            public CyAttribute2(string op)
            {
                _op = op;
            }
    
            public string Name
            {
                get { return _op; }
                set { _op = value; }
            }
        }
        /// <summary>
        /// 用户表实体类
        /// </summary>
        public class Users
        {
            [CyAttribute("System.String", 50)]
            public string Uname { get; set; }
    
            [CyAttribute("System.String", 20)]
            public string Pswd { get; set; }
    
            [CyAttribute("System.Int32", 4)]
            public int Age { get; set; }
        }
        /// <summary>
        ///常量字段
        /// </summary>
        public struct Plat
        {
            [CyAttributeField("System.String", 89)]
            public const string FirstName = "FirstName";
    
            [CyAttributeField("System.Int32", 6), CyAttribute2("hahaha")]
            public const string Age = "Age";
        }
    调用属性、字段 Attribute的方法:
    View Cod
    public void Test()
            {
                Type typetField = typeof(Plat);
                System.Reflection.FieldInfo finfo = typetField.GetField("Age");
                Attribute at1 = finfo.GetCustomAttributes(true)[0] as Attribute;
                foreach (Attribute at in finfo.GetCustomAttributes(true))
                {
    
                    CyAttributeField att = at as CyAttributeField;
                    if (att != null)
                    {
                        string dataType = att.DataType;//字段数据类型
                        int dataLen = att.Length;//字段数据长度
                    }
                }
                foreach (System.Reflection.FieldInfo fieldInfo in typetField.GetFields())
                {
                    foreach (Attribute at in fieldInfo.GetCustomAttributes(true))
                    {
                        CyAttributeField att = at as CyAttributeField;
                        if (att != null)
                        {
                            string dataType = att.DataType;//字段数据类型
                            int dataLen = att.Length;//字段数据长度
                            continue;
                        } 
                        CyAttribute2 att2 = at as CyAttribute2;
                        if (att2 != null)
                        {
                            string name2 = att2.Name; ;//字段描述 
                            continue;
                        }
                    }
                }
    
                Users user = new Users();
                user.Uname = "曹永思";
                user.Pswd = "123456";
                user.Age = 24;
    
                List<string> userInfo = new List<string>();
                Type typeProperty = typeof(Users);
    
                foreach (System.Reflection.PropertyInfo propertyInfo in typeProperty.GetProperties())
                {
                    foreach (Attribute at in propertyInfo.GetCustomAttributes(true))
                    {
                        CyAttribute att = at as CyAttribute;
                        if (att != null)
                        {
                            userInfo.Add(propertyInfo.Name + "+" + att.DataType + "+" + att.Length + "+" + att.TypeId);
                        }
                    }
                }
    
            }

     //code by:博客园-曹永思

     欢迎转载,转载请注明出处,希望帮到更多人。

    .net URL重写例子

  • 相关阅读:
    element ui 时间控件 多个日期
    java 获取两个日期之间的所有日期(年月日)
    java 正则表达式(内附例子)
    Notepad++怎么使用正则替换
    基于 vue+element ui 的cdn网站(多页面,都是各种demo)
    使用github搭建个人html网站
    PL/SQL Developer 如何记住密码
    PL/SQL Developer图形化窗口创建数据库(表空间和用户)以及相关查询sql
    安装pl/sql developer(内附下载地址)
    vue中操作cookie的插件
  • 原文地址:https://www.cnblogs.com/yonsy/p/Attribute.html
Copyright © 2020-2023  润新知