Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

.net - What's the difference or relationship between Type and TypeInfo?

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.

.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?

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?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the MSDN docs:

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.

So the Type provides a shallower representation of the object data, i.e, providing the name of the type as a string.

Where as TypeInfo provides a richer representation of the type including lists of members, implemented interfaces, and the base type.

The differences are explained in greater detail here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...