C#反射typeofGetType
⼀、typ e of G e tT yp e
参考
Unity C# 游戏开发 反射 Reflection 案例讲解(图⽂详细,带源码)
C# typeof() 和 GetType()区别
using System;
using System.Reflection;
using UnityEngine;
public class FindOfReflection : MonoBehaviour
{
private ReflectionTest reflectionTest;
private void Awake()
{
reflectionTest = new ReflectionTest();
}
void Start()
{
SeeConstructor();
SeeProperty();
SeePublicMethod();
SeePublicField();
CreatObjectByConstruct();
CreatObjByActivator();
CreatAndSet();
}
private void SeeConstructor()
{
//获取reflectionTest的Type
Type type = reflectionTest.GetType();
//通过Type获取这个类的所有构造函数信息
ConstructorInfo[] constructorArray = type.GetConstructors();
foreach (ConstructorInfo constructorInfo in constructorArray)
{
Debug.Log("=====================" + constructorInfo.ToString());
//获取每个构造函数的所有参数
ParameterInfo[] parameterArray = constructorInfo.GetParameters();
foreach (ParameterInfo parameterInfo in parameterArray)
{
Debug.Log("数据类型:" + parameterInfo.ParameterType.ToString() + "\n" + "参数名字:" + parameterInfo.Name); }
}
}
private void SeeProperty()
{
//获取reflectionTest的Type
Type type = reflectionTest.GetType();
//获取所有属性信息
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
//打印出来属性的名字
Debug.Log("属性:" + propertyInfo.Name);
}
}
private void SeePublicMethod()
{
Type type = reflectionTest.GetType();
//通过type获取所有公开⽅法的信息
MethodInfo[] methodInfos = type.GetMethods();
MethodInfo[] methodInfos = type.GetMethods();
foreach (MethodInfo methodInfo in methodInfos)
{
Debug.Log("公开⽅法的返回类型:" + methodInfo.ReturnType + "---" + "⽅法的名字:" + methodInfo.Name); }
typeof array}
private void SeePublicField()
{
Type type = reflectionTest.GetType();
//通过type获取所有公开的字段的信息
FieldInfo[] fieldInfos = type.GetFields();
foreach (FieldInfo fieldInfo in fieldInfos)
{
Debug.Log("公开的字段的名字:" + fieldInfo.Name);
}
}
/// <summary>
/// 通过反射⽤构造函数动态⽣成对象
/// </summary>
private void CreatObjectByConstruct()
{
Type type = reflectionTest.GetType();
Type[] typeArray = { typeof(string), typeof(string), typeof(string) };
//根据构造函数参数类型来获取构造函数
ConstructorInfo constructorInfo = type.GetConstructor(typeArray);
//要传⼊的参数
object[] objectArray = { "王⼆", "蘑菇", "8cm" };
//调⽤构造函数来⽣成对象
object obj = constructorInfo.Invoke(objectArray);
//调⽤打印⽅法,看是否有输出
((ReflectionTest)obj).ShowContent();
}
/// <summary>
/// 通过反射⽤Activator静态⽣成对象
/// </summary>
private void CreatObjByActivator()
{
Type type = reflectionTest.GetType();
object[] objects = { "张三", "阿姆斯特朗回旋炮", "22cm" };
object obj = Activator.CreateInstance(type, objects);
//调⽤打印⽅法,看是否有输出
((ReflectionTest)obj).ShowContent();
}
/// <summary>
/// 通过反射,创建对象,给字段,属性赋值,调⽤⽅法
/// </summary>
private void CreatAndSet()
{
Type type = reflectionTest.GetType();
//创建对象
object obj = Activator.CreateInstance(type);
//通过名字获取字段
FieldInfo fieldInfo = type.GetField("ranking");
/
/给字段赋值
fieldInfo.SetValue(obj, 1);
//获取所有属性的信息
PropertyInfo[] propertyInfos = type.GetProperties();
string[] strings = { "李四", "阿姆斯特朗回旋炮", "33cm" };
//依次给属性属性赋值
for (int i = 0; i < propertyInfos.Length; i++)
{
propertyInfos[i].SetValue(obj, strings[i], null);
}
//根据名字到⽅法的信息
MethodInfo methodInfo = type.GetMethod("StartMove");
//执⾏⽅法
methodInfo.Invoke(obj, null);
//根据名字到⽅法的信息
MethodInfo methodInfo2 = type.GetMethod("ShowRanking");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论