Unity GetMethod 获取无参数重载方法
在使用 Unity 的 GetMethod 方法获取方法时,如果有多个重载,可以通过指定方法的参数类型来获取特定的重载方法。如果想获取没有参数的重载方法,可以使用 Type.EmptyTypes 作为参数类型的参数。以下是一个示例代码:
using UnityEngine;
using System;
using System.Reflection;
public class MyClass : MonoBehaviour
{
private void MyMethod()
{
Debug.Log('No parameter method');
}
private void MyMethod(int value)
{
Debug.Log('Parameter method');
}
private void Start()
{
Type type = typeof(MyClass);
MethodInfo method = type.GetMethod('MyMethod', Type.EmptyTypes);
method.Invoke(this, null);
}
}
在上面的示例中,MyClass 类中有两个重载的 MyMethod 方法,一个没有参数,一个有一个 int 类型的参数。在 Start 方法中,使用 Type.EmptyTypes 作为参数类型的参数,可以获取没有参数的 MyMethod 方法。然后使用 MethodInfo 的 Invoke 方法调用该方法。
原文地址: https://www.cveoy.top/t/topic/qm7P 著作权归作者所有。请勿转载和采集!