I cannot fully understand when to use one or another. They are so similar that it's confusing me every now and them. When it comes to reflection, I don't know whether to use the plain Type or TypeInfo.
我不能完全理解何时使用这一个或另一个。他们非常相似,现在和他们一起让我感到困惑。说到反射,我不知道是使用普通的Type还是TypeInfo。
.NET Framework itself adds more confusion with things like assembly.DefinedTypes
. It retrieves an IEnumerable<TypeInfo>
where I supposed it would be of IEnumerable<Type>
. Also, there is a typeInfo.AsType()
method. What's the meaning of that? are they interchangeable?
.NET Framework本身增加了诸如assembly.DefinedTypes之类的混淆。它检索IEnumerable
Moreover, they have methods that are really similar.
而且,他们的方法非常相似。
It seems that TypeInfo is like a more powerful version of the classic Type
. Am I wrong?
似乎TypeInfo就像经典Type的更强大版本。我错了吗?
Thanks!
谢谢!
1 个解决方案
#1
13
From the MSDN docs:
来自MSDN文档:
A TypeInfo object represents the type definition itself, whereas a Type object represents a reference to the type definition. Getting a TypeInfo object forces the assembly that contains that type to load. In comparison, you can manipulate Type objects without necessarily requiring the runtime to load the assembly they reference.
TypeInfo对象表示类型定义本身,而Type对象表示对类型定义的引用。获取TypeInfo对象会强制加载包含该类型的程序集。相比之下,您可以操作Type对象,而无需运行时加载它们引用的程序集。
So the Type
provides a shallower representation of the object data, i.e, providing the name of the type as a string.
因此,Type提供了对象数据的浅表示,即将类型的名称作为字符串提供。
Where as TypeInfo
provides a richer representation of the type including lists of members, implemented interfaces, and the base type.
TypeInfo提供更丰富的类型表示,包括成员列表,已实现的接口和基本类型。
The differences are explained in greater detail here.
这里将更详细地解释这些差异。
#1
13
From the MSDN docs:
来自MSDN文档:
A TypeInfo object represents the type definition itself, whereas a Type object represents a reference to the type definition. Getting a TypeInfo object forces the assembly that contains that type to load. In comparison, you can manipulate Type objects without necessarily requiring the runtime to load the assembly they reference.
TypeInfo对象表示类型定义本身,而Type对象表示对类型定义的引用。获取TypeInfo对象会强制加载包含该类型的程序集。相比之下,您可以操作Type对象,而无需运行时加载它们引用的程序集。
So the Type
provides a shallower representation of the object data, i.e, providing the name of the type as a string.
因此,Type提供了对象数据的浅表示,即将类型的名称作为字符串提供。
Where as TypeInfo
provides a richer representation of the type including lists of members, implemented interfaces, and the base type.
TypeInfo提供更丰富的类型表示,包括成员列表,已实现的接口和基本类型。
The differences are explained in greater detail here.
这里将更详细地解释这些差异。