如何将"Type"类型的变量传递给泛型参数

ryu*_*ice 10 c# generics

我正在尝试这样做:

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

但它不起作用,你知道我怎么能这样做吗?

Der*_*ger 17

你需要使用反射.

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