zoukankan      html  css  js  c++  java
  • PropertyInfo、FieldInfo、MemberInfo的区别

    public class TestClass
    {
        private int a = 1;//私有一律获取不到
        public int b
        {
            get { return 2; }
            set { value = 2; }
        }
        public int c = 3;
    }
    
    public static void TestMethod()
    {
        TestClass test = new TestClass();
        PropertyInfo[] pro = test.GetType().GetProperties();
        FieldInfo[] fil = test.GetType().GetFields();
        MemberInfo[] men = test.GetType().GetMembers();
    
        foreach (var item in pro)//仅能获取到b属性(输出b=2)
        {
            Console.WriteLine("PropertyInfo: " + item.Name  +"=" + item.GetValue(test, null));
        }
        foreach (FieldInfo item in fil)//仅能获取到c字段(输出c=2)
        {
            Console.WriteLine("FieldInfo: " + item.Name + "=" + item.GetValue(test));
        }
        foreach (MemberInfo item in fil)//仅能获取到c字段(输出c)
        {
            Console.WriteLine("MemberInfo: "+ item.Name );
        }
    }

    问题:

    [多选] 哪个对象的类型来源于MemberInfo类?()

    A . FieldInfo class
    B . MethodInfo class
    C . Assembly class
    D . Type cla

    参考答案: A, B, D

    参考

    [整理]C#反射(Reflection)详解

  • 相关阅读:
    结构体
    out传值
    函数
    数组
    计算公式
    MYSQLinsert速度过慢
    Centos6.4 本地yum源配置
    JProfiler解决Java服务器的性能跟踪
    Hessian Servlet和Hessian Spring的简单应用
    Jetty实战之 安装 运行 部署
  • 原文地址:https://www.cnblogs.com/code1992/p/11348064.html
Copyright © 2011-2022 走看看