C#类型或命名空间预期混乱 [英] C# type or namespace expected confusion
本文介绍了C#类型或命名空间预期混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即时得到小幅一个C#的错误混淆。
Im getting slightly confused by a C# error.
类型T = thing.GetType()
T
现在是一个类型。但是,如果我试图做到这一点:
t
is now a type. but if i attempt to do this:
新GenericThing< T>
我得到一个警告,预期的类型或命名空间。我失去了什么?
I get a warning saying type or namespace expected. What am i missing?
推荐答案
T
是一类对象的创建运行。预计泛型类型名称,在编译时解析。要在运行时创建一个通用的,你必须使用 MakeGenericType
t
is a Type object created at runtime. Generics expect a type name, resolved at compile time. To create a generic at runtime, you have to use MakeGenericType
例如:
Activator.CreateInstance(typeof(GenericThing<>).MakeGenericType(t));
这篇关于C#类型或命名空间预期混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文