C#反射的学习(一)--获得成员信息

在学习反射的时候,看到这篇文章了。写的通俗易懂,于是自己测试了结果。

首先是使用Type.GetMembers()

使用到的类例子

public class Person
{
    private int _test3;
    private int _test1 { get; set; }
    protected int Test2 { get; set; }
    public int Test3 { get; set; }

    public void Show()
    {
    }

    private static void Show2()
    {
    }

    public static void Show3()
    {
    }
}
假设上面的类成员我们并不知道。那么通过反射获得成员方法如下:

using UnityEngine;
using System.Collections;
using System;
using System.Reflection;

public class Reflection_Test : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        Type t = typeof(Person);
        
        //GetMembers()默认返回公共成员 包括父类的成员
        MemberInfo[] minfos = t.GetMembers();
        foreach (MemberInfo m in minfos)
        {
            Debug.Log(m.Name);
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}
打印输出:




using UnityEngine;
using System.Collections;
using System;
using System.Reflection;

public class Reflection_Test : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        Type t = typeof(Person);
        
        //包含非公共成员 包含实例成员 包含公共成员
        MemberInfo[] minfos2 = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
        foreach (MemberInfo m in minfos2)
        {
            Debug.Log(m.Name);
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}
打印输出:



using UnityEngine;
using System.Collections;
using System;
using System.Reflection;

public class Reflection_Test : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        Type t = typeof(Person);
        
        //指定只应考虑在所提供类型的层次结构级别上声明的成员。 不考虑继承的成员
        MemberInfo[] minfos3 = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
        foreach (MemberInfo m in minfos3)
        {
            Debug.Log(m.Name);
        }   
    }

    // Update is called once per frame
    void Update()
    {

    }
}
打印输出:



using UnityEngine;
using System.Collections;
using System;
using System.Reflection;

public class Reflection_Test : MonoBehaviour
{
    public int whichDebug = 0;
    // Use this for initialization
    void Start()
    {
        Type t = typeof(Person);
       
        //输出静态成员
        MemberInfo[] minfos3 = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
        foreach (MemberInfo m in minfos3)
        {
            Debug.Log(m.Name);
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}
打印输出:



using UnityEngine;
using System.Collections;
using System;
using System.Reflection;

public class Reflection_Test : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        Type t = typeof(Person);
        
        //输出成员的类型
        //输出成员的类型
        //public delegate TResult Func
   
   
    
    (T arg);
        //封装一个具有一个参数并返回 TResult 参数指定的类型值的方法
        //这里可以学习到 http://www.cnblogs.com/pnljs/p/3792407.html
        Func
    
    
     
      getType = (x) =>
        {
            switch (x)
            {
                case MemberTypes.Field:
                    return "字段";
                case MemberTypes.Method:
                    return "方法";
                case MemberTypes.Property:
                    return "属性";
                default: return "未知";
            }
        };

        MemberInfo[] minfos3 = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static);
        foreach (MemberInfo m in minfos3)
        {
            Debug.Log(m.Name + "  类型: " + getType(m.MemberType));
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}
    
    
   
   
打印输出:



代码中的枚举说明见这里BindingFlags和这里MemberTypes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值