简要介绍:
目前正在开发Unity Whit C#的视频游戏/Leraning项目,我遇到了一些问题,无法使用System.Windows.Forms.Forms既不是系统。
我希望这个时钟每个tick都有动态呼叫者,因此,当调用来自另一个对象的方法时,使用system。反射和许多博客和网站,我找到了如何使其从同一对象调用方法whit whit whit unity调用,我希望它可以调用游戏中的任何方法,例如system.windows.forms,此时我要这样做:
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,当我将tick设置为 clock.set_tick(“时钟”,“ test”,clock);
这应该执行方法时钟。测试,但由于某种原因,它无法找到该方法,我尝试了几乎所有内容,也可以在堆栈,但它们适合这个问题,想法?
编辑
完整错误代码:
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)
Brief introduction:
Currently developing a videogames/leraning project in unity whit c#, i have some issues and cant use system.windows.forms neither the system.therading, so i decide to make my own clock and have more control.
i want that this clock have a dynamic caller per tick, so when it ticks a method from another object is called, using system.reflection and many blogs and sites for reference i found how to make it call methods from the same object whit the unity invoke, i want this to call any method in the game, like the system.windows.forms, at this point i make this:
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");
}
the issue here is the method isnt capable of finding the method tick, when i set the tick to clock.Set_Tick("Clock", "TEST", clock);
this should execute the method Clock.TEST but for some reason it is incapable of finding the method, i try almost everything and also read similar cuestions in stack but they arent suited for this issue, ideas?
Edit
Full Error code:
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)
发布评论
评论(2)
您需要再提供一个标志 -
bindingflags.instance
向调用:You need to provide one more flag -
BindingFlags.Instance
to the invoke call:在调用
InvokeMember
的调用中,您没有指定该方法应搜索对象实例的所有成员。添加bindingflags.instance
作为附加的绑定标志:操作更快(至少用于多个调用),并且比反射更可读。这是您的样本外观。
clock.cs
program.cs
In your call to
InvokeMember
you are not specifying that the method should search all members of the object instance. AddBindingFlags.Instance
as an additional binding flag:Action is faster (at least for multiple invocations) and more readable than Reflection. Here's what your sample would look like with Action.
Clock.cs
Program.cs