Brief introduction:
我希望这个时钟在每个刻度上都有一个动态调用者,所以当它刻度时,会使用system调用另一个对象的方法.反射和许多博客和网站作为参考,我发现了如何让它调用来自同一个对象的方法来调用unity调用,我希望这可以调用游戏中的任何方法,比如系统.windows .表格,在这一点上,我提出:
public object InvokeByName(string typeName, string methodName, object target)
{
Type callType = Type.GetType(typeName);
Binder defaultBinder = Type.DefaultBinder;
return callType.InvokeMember(methodName,
BindingFlags.InvokeMethod | BindingFlags.Public,
defaultBinder, target , new object[] { });
}
public void Tick(string method, string methodName, object target)
{
InvokeByName(method, methodName, target);
}
public void Set_Tick(string typeName, string methodName, object target)
{
this.typeName = typeName;
this.methodName = methodName;
this.target = target;
}
public void TEST()
{
Debug.Log("TEST");
}
这里的问题是,当我将tick设置为clock.Set_Tick("Clock", "TEST", clock);
时,该方法无法找到方法tick
这应该执行方法时钟.测试但由于某种原因,它无法找到方法,我try 了几乎所有的方法,也阅读了堆栈中类似的提示,但它们不适合这个问题, idea ?
Edit
MissingMethodException: Method 'Clock.TEST' not found.
System.RuntimeType.InvokeMember (System.String name,
System.Reflection.BindingFlags bindingFlags, System.Reflection.Binder
binder, System.Object target, System.Object[] providedArgs,
System.Reflection.ParameterModifier[] modifiers,
System.Globalization.CultureInfo culture, System.String[] namedParams)
(at <d4cde64232cf45659d86aafa597faa77>:0)
System.Type.InvokeMember (System.String name,
System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder
binder, System.Object target, System.Object[] args) (at
<d4cde64232cf45659d86aafa597faa77>:0)
Clock.InvokeByName (System.String typeName, System.String methodName,
System.Object target) (at Assets/Scripts/Clock.cs:31)
Clock.Tick (System.String method, System.String methodName,
System.Object target) (at Assets/Scripts/Clock.cs:38)
Clock.FixedUpdate () (at Assets/Scripts/Clock.cs:58)