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)
CodePudding user response:
You need to provide one more flag - BindingFlags.Instance
to the invoke call:
return callType.InvokeMember(methodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
defaultBinder, target, new object[] { });
CodePudding user response:
In your call to InvokeMember
you are not specifying that the method should search all members of the object instance. Add BindingFlags.Instance
as an additional binding flag:
return callType.InvokeMember(methodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
defaultBinder, target , new object[] { }
);