zoukankan      html  css  js  c++  java
  • 反射与特性

    示例代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAttribute.Extension
    {
        /// <summary>
        /// 用户状态
        /// </summary>
        public enum UserState
        {
            /// <summary>
            /// 正常
            /// </summary>
            [Remark("正常")]
            Normal = 0,//左边是字段名称  右边是数据库值   哪里放描述?  注释
            /// <summary>
            /// 冻结
            /// </summary>
            [Remark("冻结")]
            Frozen = 1,
            /// <summary>
            /// 删除
            /// </summary>
            //[Remark("删除")]
            Deleted = 2
        }
        //枚举项加一个描述   实体类的属性也可以Display  
        //别名   映射  
        public class RemarkAttribute : Attribute
        {
            public RemarkAttribute(string remark)
            {
                this._Remark = remark;
            }
            private string _Remark = null;
            public string GetRemark()
            {
                return this._Remark;
            }
        }
    
    
        public static class RemarkExtension
        {
            public static string GetRemark(this Enum value)
            {
                Type type = value.GetType();
                FieldInfo field = type.GetField(value.ToString());
                if (field.IsDefined(typeof(RemarkAttribute), true))
                {
                    RemarkAttribute attribute = (RemarkAttribute)field.GetCustomAttribute(typeof(RemarkAttribute), true);
                    return attribute.GetRemark();
                }
                else
                {
                    return value.ToString();
                }
            }
    
        }
    }
    using MyAttribute.Extension;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAttribute
    {
        /// <summary>
        /// 这里是注释,除了让人看懂这里写的是什么,
        /// 对运行没有任何影响
        /// </summary>
        //[Obsolete("请不要使用这个了,请使用什么来代替", true)]//影响编译器的运行
        //[Serializable]//可以序列化和反序列化  可以影响程序的运行
        //MVC filter   ORM table  key  display   wcf 
    
        //[Custom]
        //[Custom()]
        //[Custom(123), Custom(123, Description = "1234")]
        [Custom(123, Description = "1234", Remark = "2345")]//方法不行
        public class Student
        {
            [CustomAttribute]
            public int Id { get; set; }
            [Leng(5,10)]//还有各种检查
            public string Name { get; set; }
            [Leng(20, 50)]
            public string Accont { get; set; }
    
            /// <summary>
            /// 10001~999999999999
            /// </summary>
            [Long(10001, 999999999999)]
            public long QQ { get; set; }
    
    
            //private long _QQ2 = 0;//解决数据合法性,给属性增加了太多的事儿
            //public long QQ2
            //{
            //    get
            //    {
            //        return this._QQ2;
            //    }
            //    set
            //    {
            //        if (value > 10001 && value < 999999999999)
            //        {
            //            _QQ2 = value;
            //        }
            //        else
            //        {
            //            throw new Exception();
            //        }
            //    }
            //}
            [CustomAttribute]
            public void Study()
            {
                Console.WriteLine($"这里是{this.Name}");
            }
    
            [Custom()]
            [return: Custom()]
            public string Answer([Custom]string name)
            {
                return $"This is {name}";
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAttribute.Extension
    {
        public static class ValidateExtension
        {
            public static bool Validate(this object oObject)
            {
                Type type = oObject.GetType();
                foreach (var prop in type.GetProperties())
                {
                    if (prop.IsDefined(typeof(AbstractValidateAttribute), true))
                    {
                        object[] attributeArray = prop.GetCustomAttributes(typeof(AbstractValidateAttribute), true);
                        foreach (AbstractValidateAttribute attribute in attributeArray)
                        {
                            if (!attribute.Validate(prop.GetValue(oObject)))
                            {
                                return false;//表示终止
                            }
                        }
                    }
    
                    //if (prop.IsDefined(typeof(LongAttribute), true))
                    //{
                    //    LongAttribute attribute = (LongAttribute)prop.GetCustomAttribute(typeof(LongAttribute), true);
                    //    if (!attribute.Validate(prop.GetValue(oObject)))
                    //    {
                    //        return false;
                    //    }
                    //}
                    //if (prop.IsDefined(typeof(LengAttribute), true))
                    //{
                    //    LengAttribute attribute = (LengAttribute)prop.GetCustomAttribute(typeof(LengAttribute), true);
                    //    if (!attribute.Validate(prop.GetValue(oObject)))
                    //    {
                    //        return false;
                    //    }
                    //}
                }
                return true;
            }
        }
    
        public abstract class AbstractValidateAttribute : Attribute
        {
            public abstract bool Validate(object value);
        }
    
        [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
        public class LengAttribute : AbstractValidateAttribute
        {
            private int _Min = 0;
            private int _Max = 0;
            public LengAttribute(int min, int max)
            {
                this._Min = min;
                this._Max = max;
            }
    
    
            public override bool Validate(object value)//" "
            {
                if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
                {
                    int length = value.ToString().Length;
                    if (length > this._Min && length < this._Max)
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        public class LongAttribute : AbstractValidateAttribute
        {
            private long _Min = 0;
            private long _Max = 0;
            public LongAttribute(long min, long max)
            {
                this._Min = min;
                this._Max = max;
            }
    
    
            public override bool Validate(object value)//" "
            {
                //可以改成一句话
                if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
                {
                    if (long.TryParse(value.ToString(), out long lResult))
                    {
                        if (lResult > this._Min && lResult < this._Max)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
    }
  • 相关阅读:
    NGUI Sprite 和 Label 改变Layer 或父物体后 未更新深度问题
    unity销毁层级物体及 NGUI 深度理解总结
    unity 2d 和 NGUI layer
    关于NGUI与原生2D混用相互遮盖的问题心得
    CentOS7为firewalld添加开放端口及相关操作
    Python 操作redis有序集合(sorted set)
    win10下安装redis 服务
    python2/3中 将base64数据写成图片,并将图片数据转为16进制数据的方法、bytes/string的区别
    解决最小化安装Centos7后无法上网的问题,以及安装成功后的基本配置
    在mysql中使用group by和order by取每个分组中日期最大一行数据,亲测有效
  • 原文地址:https://www.cnblogs.com/cxxtreasure/p/13258108.html
Copyright © 2011-2022 走看看