如何使用多个参数使用makeGenericMethod

发布于 2025-01-22 21:09:42 字数 826 浏览 1 评论 0原文

我正在尝试使用字符串(用户输入)在运行时解析的类型参数调用通用方法。这是我的测试代码:

        MethodInfo method = typeof(GameManager).GetMethod(nameof(GameManager.SetPreference));
        MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { typeof(PlayerData.Preferences), typeof(bool) });
        genericMethod.Invoke(new GameManager(), new object[] { PlayerData.Preferences.WaitMode, true });

这将失败,“ grigentException:不正确的长度”。

这是我称之为的功能:

public void SetPreference<T>(PlayerData.Preferences preference, T value)
    {
        try
        {
            PlayerData.SetAttr(preference.ToString(), value);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            return;
        }

        OnPreferenceChanged.Raise(preference);
    }

我在做什么错?

I am trying to invoke a generic method using a type argument that is parsed at runtime from a string (user input). Here is my test code:

        MethodInfo method = typeof(GameManager).GetMethod(nameof(GameManager.SetPreference));
        MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { typeof(PlayerData.Preferences), typeof(bool) });
        genericMethod.Invoke(new GameManager(), new object[] { PlayerData.Preferences.WaitMode, true });

This fails with "ArgumentException: Incorrect length".

Here is the function I'm calling:

public void SetPreference<T>(PlayerData.Preferences preference, T value)
    {
        try
        {
            PlayerData.SetAttr(preference.ToString(), value);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            return;
        }

        OnPreferenceChanged.Raise(preference);
    }

What am I doing wrong?

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

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

发布评论

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

评论(1

美人骨 2025-01-29 21:09:42

MakeGenericMethod的参数是针对目标通用方法的 type-parameters 而不是方法参数,在您的情况下,set> setpReference /code>方法只有1个类型参数:t,而不是2。

对于setpReference&lt; boolean&gt;仅通过new Type> new Type [] {typeof(boolean)} - 因此,请勿通过typef(playerData.preferences) makegenericMethod

MethodInfo method = typeof(GameManager).GetMethod(nameof(GameManager.SetPreference));
MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { typeof(bool) });
genericMethod.Invoke(new GameManager(), new object[] { PlayerData.Preferences.WaitMode, true });

MakeGenericMethod's parameters are for the type-parameters of the target generic method not the method parameters, in your case the SetPreference method only has 1 type-parameter: T, not 2.

For SetPreference<Boolean> pass only new Type[] { typeof(Boolean) } - so don't pass typeof(PlayerData.Preferences) to MakeGenericMethod.

MethodInfo method = typeof(GameManager).GetMethod(nameof(GameManager.SetPreference));
MethodInfo genericMethod = method.MakeGenericMethod(new Type[] { typeof(bool) });
genericMethod.Invoke(new GameManager(), new object[] { PlayerData.Preferences.WaitMode, true });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文