zoukankan      html  css  js  c++  java
  • 反射判断某一个属性是否只读或者只写

    .Net Framework 4.0 以下,包括4.0

                Type pPt = typeof(People);
                System.Reflection.PropertyInfo[] tPro = pPt.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var d in tPro)
                {
                    //属性只读判断
                    if (d.GetGetMethod() != null && d.GetSetMethod()==null)
                    {
                        
                    }
                    //属性只写判断
                    if (d.GetGetMethod() == null && d.GetSetMethod() != null)
                    {
    
                    }
                }
                Console.Read();

    .Net Framework 4.5 以上,包括4.5

                Type pPt = typeof(People);
                System.Reflection.PropertyInfo[] tPro = pPt.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var d in tPro)
                {
                    //属性只读判断
                    if (d.GetMethod != null && d.SetMethod==null)
                    {
                        Console.WriteLine(d.Name + ":只读");
                    }
                    //属性只写判断
                    if (d.GetMethod == null && d.SetMethod != null)
                    {
                        Console.WriteLine(d.Name + ":只写");
                    }
                }
  • 相关阅读:
    Windows Phone 7 电话本相关的操作
    Windows CE 下判断 URL 地址是否有效
    cigarettes
    开灯问题
    日期计算
    小学生算术
    另一种阶乘问题
    鸡兔同笼
    笨小熊
    6174问题
  • 原文地址:https://www.cnblogs.com/cglNet/p/5138447.html
Copyright © 2011-2022 走看看