1. The concept of reflection
Use the type class to detect fields, structures, properties, methods, etc. in a class
public class Test : MonoBehaviour
{
private void Start()
{
TestType();
}
public static void TestType()
{
//typeof
Type type = typeof(String);
//侦测类的构造
foreach (var item in type.GetConstructors())
{
Debug.Log(item);
}
//侦测属性
foreach (var item in type.GetProperties())
{
Debug.Log(item);
}
//侦测方法
foreach (var item in type.GetMethods())
{
Debug.Log(item);
}
}
}
2. Reflective core type
1. obj.GetType(): Get the Type object corresponding to the prototype type
public class Test : MonoBehaviour
{
private void Start()
{
TestType("123");
}
public static void TestType(System.Object obj)
{
//obj.GetType():获取原型类型对应的Type对象
Type type = obj.GetType();
//侦测类的构造
foreach (var item in type.GetConstructors())
{
Debug.Log(item);
}
//侦测属性
foreach (var item in type.GetProperties())
{
Debug.Log(item);
}
//侦测方法
foreach (var item in type.GetMethods())
{
Debug.Log(item);
}
}
}
2. (i) Get Type through class name
(ii) Pass a full class name
public class Test : MonoBehaviour
{
private void Start()
{
TestType("System.Object");
}
//传过一个类全称
public static void TestType(string className)
{
//通过类名称获取Type
Type type = Type.GetType(className);
//侦测类的构造
foreach (var item in type.GetConstructors())
{
Debug.Log(item);
}
//侦测属性
foreach (var item in type.GetProperties())
{
Debug.Log(item);
}
//侦测方法
foreach (var item in type.GetMethods())
{
Debug.Log(item);
}
}
}