zoukankan      html  css  js  c++  java
  • 反射的使用及其使用场景

    1、什么是反射
      在.net程序运行的时候会将各种对象中的数据、对象的名称、对象的类型这些信息保存在元数据中,元数据保存在程序集中,我们访问并操作元数据或者程序集的行为就叫反射。举个栗子:我们在代码中实例化的对象是一座房子,但是程序编译运行时的基本单位不是房子,而是砖。程序把这座房子解析成了一块块砖,我们访问、操作这些砖需要把它还原成一座房子。具体来说,把一个对象解析成元数据,再把元数据还原成另一个对象的行为叫反射,但是只要访问了,就使用了对象,所以我说访问并操作元数据或者程序集的行为就叫反射。当然了,这是个人理解。

    2、如何使用反射
      使用GetType()或者typeof()方法获取一个type类,这个类就包含了该对象的所有信息。下面用代码举例一些反射的基本用法:

    /// <summary>
    /// 反射用的类
    /// </summary>
    public class TypeofModel
    {
        [Description("这是id")]
        public int Id { get; set; }
        [Description("这是编码")]
        public string No { get; set; }
    }
    //下面是调用的代码
    var typeofModel = new TypeofModel() { Id = 1, No = "第一" };
    var t = typeofModel.GetType();
    var cols = t.GetFields();//获取所有字段
    var properties = t.GetProperties();//获取所有属性
    foreach (var v in properties)
    {
        string str = "";
        str += v.Name;//获取属性名称
        str += v.PropertyType;//获取属性类型
        str += v.GetValue(typeofModel);//获取属性值
        object[] attributes = v.GetCustomAttributes(true);//获取该属性值的所有特性
        foreach (var m in attributes)
        {
            var childProperties = m.GetType().GetProperties();//获取所有属性
            foreach (var n in childProperties)
            {
                str += n.GetValue(m);//获取属性值
            }
        }
    }
    

    3、反射的应用场景
      保存数据的时候可以使用反射验证数据的简单信息,这里我们只验证string类型,代码如下:

    public class ValidationAttribute : Attribute
    {
        public ValidationAttribute(int maxLength, string name, bool isRequired)
        {
            MaxLength = maxLength;
            Name = name;
            IsRequired = isRequired;
        }
        public int MaxLength { get; set; }
        public string Name { get; set; }
        public bool IsRequired { get; set; }
    }
    
    public static class ValidationModel
    {
        public static (bool,string) Validate(object obj)
        {
            var t = obj.GetType();
            var properties = t.GetProperties();//获取所有属性
            foreach (var property in properties)
            {
                if (!property.IsDefined(typeof(ValidationAttribute), false)) continue;//验证是否加上ValidationAttribute标记
                object[] objs = property.GetCustomAttributes(typeof(ValidationAttribute), true);//获取特性
                var firstValidation = (ValidationAttribute)objs[0];
                var maxLength = firstValidation.MaxLength;
                var name = firstValidation.Name;
                var isRequired = firstValidation.IsRequired;
    
                //获取属性的值
                var propertyValue = property.GetValue(obj) as string;
                if (string.IsNullOrEmpty(propertyValue) && isRequired)
                {
                    return (false, $"{name}不可为空");
                }
                if (!string.IsNullOrEmpty(propertyValue) && propertyValue.Length > maxLength)
                {
                    return (false, $"{name}长度不可超过{maxLength}");
                }
            }
            return (true,"");
        }
    }
    
    /// <summary>
    /// 反射用的类
    /// </summary>
    public class TypeofModel
    {
        [Description("这是id")]
        public int Id { get; set; }
        [Validation(64,"编码",true)]
        public string No { get; set; }
    }
    
    var model = new TypeofModel() { Id = 1, No = "" };
    var msg = ValidationModel.Validate(model);
    if (!msg.Item1) { throw new Exception(msg.Item2); }
    

      

  • 相关阅读:
    NGINX不允许向静态文件提交POST方式的请求,否则报405错误(apache中没有出现)
    nginx反向代理做cache配置
    Apache与Nginx的优缺点比较
    Nginx配置文件详细说明
    Nginx 配置指令的执行顺序
    不可忽略的apache 的 Keep Alive
    HTTP Keep-Alive详解
    跳转 nginx 跳转 apache跳转
    apache重定向无效
    WebUploader 超大文件上传解决方案:分片断点上传(一)
  • 原文地址:https://www.cnblogs.com/liangshibo/p/13311222.html
Copyright © 2011-2022 走看看