Using Type objects as Type Parameters for Generics in C#

后端 未结 2 1930
北恋
北恋 2020-12-19 07:42

I cannot find a way to use a first-class Type object (System.Type instance) as a Type Parameter in a generic construction in C# 3.0 / .NET 3.5. Below is a simplified example

2条回答
  •  长情又很酷
    2020-12-19 08:27

    You're trying to determine your type argument at runtime. .Net generics require that the type arguments are known at compile time. There's really no way to do that outside of switching on the type. You may be able to get a handle to the method via reflection and invoke it, but that's ugly.

提交回复
热议问题