动态呼叫者给出错误缺少methodexception:方法' clock.test'未找到

发布于 2025-02-07 19:57:02 字数 2115 浏览 1 评论 0 原文

简要介绍: 目前正在开发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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倚栏听风 2025-02-14 19:57:02

您需要再提供一个标志 - bindingflags.instance 向调用:

return callType.InvokeMember(methodName,
            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
            defaultBinder, target, new object[] { });

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[] { });
旧时浪漫 2025-02-14 19:57:02

在调用 InvokeMember 的调用中,您没有指定该方法应搜索对象实例的所有成员。添加 bindingflags.instance 作为附加的绑定标志:

return callType.InvokeMember(methodName, 
   BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, 
   defaultBinder, target , new object[] { }
);

操作更快(至少用于多个调用),并且比反射更可读。这是您的样本外观。

clock.cs

public class Clock
{
    private Action TickAction = null!;
    
    public void Tick()
    {
        TickAction();
    }

    public void Set_Tick(Action tick)
    {
        TickAction = tick;
    }

    public void TEST()
    {
        Console.Write("TEST");
    }    
}

program.cs

var clock = new Clock();
clock.Set_Tick(clock.TEST);
clock.Tick();

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[] { }
);

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

public class Clock
{
    private Action TickAction = null!;
    
    public void Tick()
    {
        TickAction();
    }

    public void Set_Tick(Action tick)
    {
        TickAction = tick;
    }

    public void TEST()
    {
        Console.Write("TEST");
    }    
}

Program.cs

var clock = new Clock();
clock.Set_Tick(clock.TEST);
clock.Tick();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文