在学习反射的时候,看到这篇文章了。写的通俗易懂,于是自己测试了结果。
首先是使用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