如何传递类型“Type”的变量泛型参数 [英] How to pass variable of type "Type" to generic parameter

查看:388
本文介绍了如何传递类型“Type”的变量泛型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  Type type = Type.GetType(string.Format(Gestor.Data .Entities。{0},Gestor.Data,e.Item.Value)); 
MetaDataUtil.GetColumnasGrid< type>();

但它不起作用,您有任何想法我可以做到这一点吗?

解决方案

您需要为此使用反射。

  var method = 
typeof(MetaDataUtil)
.GetMethod(GetColumnasGrid)
.MakeGenericMethod(new [] {type})
.Invoke(null,null);


I'm trying to do this:

Type type = Type.GetType(string.Format("Gestor.Data.Entities.{0}, Gestor.Data", e.Item.Value));
MetaDataUtil.GetColumnasGrid<type>();

but it's not working, do you have any idea how I could do this?

解决方案

You need to use reflection for this.

var method =
    typeof(MetaDataUtil)
    .GetMethod("GetColumnasGrid")
    .MakeGenericMethod(new [] { type })
    .Invoke(null, null);

这篇关于如何传递类型“Type”的变量泛型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆