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重写例子

原文地址:https://www.cnblogs.com/yonsy/p/Attribute.html