传递实例化的 System.Type 作为泛型方法的类型参数

发布于 2024-09-26 10:57:15 字数 1199 浏览 6 评论 0原文

我有一个想要简化的代码片段:

        switch (typeString)
        {
            case "boolean":
                CreateSimpleRows<bool>(ref group, value);
                break;
            case "datetime":
                CreateSimpleRows<DateTime>(ref group, value);
                break;
            case "double":
                CreateSimpleRows<double>(ref group, value);
                break;
            case "int32":
                CreateSimpleRows<int>(ref group, value);
                break;
            case "int64":
                CreateSimpleRows<long>(ref group, value);
                break;
            case "string":
                CreateSimpleRows<string>(ref group, value);
                break;
        }

该方法声明为 CreateSimpleRows。我尝试传递 System.Type 实例,但这不起作用。

我在类似问题上找到了这个答案: 传递将 System.Type 实例化为泛型类的类型参数

我检查过,发现 MethodInfo 类中有一个 MakeGenericMethod。 问题是,我不知道如何将“CreateSimpleRows”转换为 MethodInfo 实例。

我想要实现的目标是否可能实现? 预先感谢您的回复。

I have this code snippet that I want to simplify:

        switch (typeString)
        {
            case "boolean":
                CreateSimpleRows<bool>(ref group, value);
                break;
            case "datetime":
                CreateSimpleRows<DateTime>(ref group, value);
                break;
            case "double":
                CreateSimpleRows<double>(ref group, value);
                break;
            case "int32":
                CreateSimpleRows<int>(ref group, value);
                break;
            case "int64":
                CreateSimpleRows<long>(ref group, value);
                break;
            case "string":
                CreateSimpleRows<string>(ref group, value);
                break;
        }

The method is declared as CreateSimpleRows<T>. I tried passing a System.Type instance, but that didn't work.

I came across this answer to a similar question:
Pass An Instantiated System.Type as a Type Parameter for a Generic Class

I've checked and I've seen that there's a MakeGenericMethod in the MethodInfo class.
Thing is, I don't know how to convert "CreateSimpleRows" into a MethodInfo instance.

Is what I'm thinking of achieving even possible?
Thanks in advance for the replies.

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

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

发布评论

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

评论(1

冷情 2024-10-03 10:57:15

要获取 MethodInfo,请调用 < code>Type.GetMethod

MethodInfo method = typeof(TypeContainingMethod).GetMethod("CreateSimpleRows");
MethodInfo generic = method.MakeGenericMethod(typeArgument);

请注意,如果您想获取非公共方法,则需要使用 GetMethod 的重载,该重载采用 BindingFlags 也是如此。

不过,目前还不清楚为什么要通过反思来做到这一点。虽然您当前的代码片段是重复的,但至少很容易理解。使用反射可能会使事情更容易出错,并且您仍然必须首先将 typeString 映射到 Type

To get a MethodInfo, you call Type.GetMethod:

MethodInfo method = typeof(TypeContainingMethod).GetMethod("CreateSimpleRows");
MethodInfo generic = method.MakeGenericMethod(typeArgument);

Note that if you want to get a non-public method you'll need to use the overload of GetMethod which takes a BindingFlags as well.

It's not really clear why you want to do this with reflection though. While your current snippet is repetitive, it's at least simple to understand. Using reflection is likely to make things more error-prone, and you'd still have to map typeString to a Type to start with.

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