效果:
直接上菜了喂
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class MyReflection : EditorWindow
{
/*
* 反射获取私有方法
*/
[MenuItem("MyEditor/反射/MyReflectionTest/GetName()")]
public static void Execute()
{
Type type = typeof(MyReflectionTest);
object obj = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("GetName", BindingFlags.NonPublic | BindingFlags.Instance,null, CallingConventions.Standard, new Type[] { },null);
method.Invoke(obj,null);
}
[MenuItem("MyEditor/反射/MyReflectionTest/GetName(string name)")]
public static void Execute1()
{
Type type = typeof(MyReflectionTest);
object obj = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("GetName", BindingFlags.NonPublic | BindingFlags.Instance,null, CallingConventions.Standard, new Type[] {typeof(string) }, null);
object[] parameters = new object[] { "LF" };
method.Invoke(obj, parameters);
}
[MenuItem("MyEditor/反射/MyReflectionTest/GetName(string name,int age)")]
public static void Execute2()
{
Type type = typeof(MyReflectionTest);
object obj = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("GetName", BindingFlags.NonPublic | BindingFlags.Instance,null, CallingConventions.Standard, new Type[] {typeof(string) , typeof(int) }, null);
object[] parameters = new object[] { "LF", 18 };
method.Invoke(obj, parameters);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyReflectionTest : MonoBehaviour
{
private string _name = "PCJ";
private void GetName()
{
Debug.Log("我的名字:" + _name);
}
private void GetName(string _name)
{
Debug.Log("我的名字:" + _name);
}
private void GetName(string _name, int _age)
{
Debug.Log("我的名字:" + _name + " 我的岁数:" + _age);
}
}
来源:CSDN
作者:不做秃鹰
链接:https://blog.csdn.net/qq_41211080/article/details/103618802