What returns GetType () in an instance of Type?

I used this code during some debugging.

private bool HasBaseType(Type type, out Type baseType) { Type originalType = type.GetType(); baseType = GetBaseType(type); return baseType != originalType; } 

My initial thought was that type.GetType() in the first line of the method always gives the equivalent of typeof(System.Type) , which seems completely useless to me in context. However, MSDN suggests that Type overrides the version of GetType() that will be inherited from Object . Then the actual MSDN page on a non-static type.GetType() (not to be confused with the three static versions) says that the GetType() method returns "Current Type". No further relevant explanation is provided.

So, is the value of originalType in the method above equal to Type or typeof(System.Type) ? I'm not quite sure about the documentation. If originalType equivalent to Type , it will be a copy of Type , so if the GetBaseType method changes its parameter, will originalType still be equivalent to Type in its original form, regardless of what happens inside GetBaseType ?

+1
source share
3 answers

one.

GetType is not defined as virtual and therefore cannot be overridden. System.Type does not override the GetType object, it hides it (in C # it will be represented by a new keyword). If you use a disassembly tool such as ILSpy, you will see that it is implemented as follows:

 public new Type GetType() { return base.GetType(); } 

As you can see, it only calls the GetType call of the base class (which is the object), so the end result is the same. This version of GetType provides a formal implementation of the _Type interface. As a rule, you should not worry about this interface, it is there for compatibility reasons.

2.

The GetType method is defined in the object and returns System.Type. But a closer look at System.Type reveals that it is defined as abstract, so it can never be created. So the way to get something specific from GetType is to get another type that comes from System.Type. Indeed, we get an instance of System.RuntimeType that comes from System.Type.

The reason for this is that there should be only one instance that represents the given type. That is, no matter how many different instances of the string (for example) you call GetType, you will still get the same instance, because there is only one instance that describes the string. To make sure that there is only one instance, System.Type was defined as abstract, so it cannot be created using user code, and System.RuntimeType is defined as internal, therefore it is only available for code inside mscorlib and cannot be created either user code.

3.

The existence of System.RuntimeType is an implementation detail that in most cases can be ignored. In all senses and purposes, your code may assume that GetType returns an instance of System.Type. The one exception is the following code (you can replace the string with any other type):

 bool b = typeof(string).GetType() == typeof(Type); 

If you do not know System.RuntimeType, you can assume that b will be true because he suggested that GetType returns System.Type, which is clearly the value of the right-hand expression. But since the value of the left hand is actually System.RuntimeType, b is false (System.Type and System.RuntimeType are two different types and therefore not equal). This is the only time you need to know System.RuntimeType to understand why the code behaves the way it does. In any other case, it just doesn't matter that GetType returns System.RuntimeType, and you can ignore it.

4.

Here is a direct link to part of my online .NET course that discusses runtime type information (RTTI). I do not mention System.RuntimeType because, as I said, these are implementation details that for the most part can be ignored. But this link will give you more information and a clearer understanding of System.Type.

http://motti.me/tw

Hope this helps!

Mochi

+6
source

The type instance type is System.RuntimeType.

  { Console.WriteLine(typeof(System.Type).GetType()); Console.WriteLine(typeof(string).GetType().GetType()); Console.WriteLine(typeof(int).GetType()); Console.WriteLine(typeof(List<int>).GetType()); } System.RuntimeType System.RuntimeType System.RuntimeType System.RuntimeType 
+3
source

It seems like not. When you call GetType on an instance of Type , it always returns RuntimeType .

I checked this on the system build by executing this piece of code:

 Assembly .GetAssembly(typeof(int)) .GetTypes() .Where(type => type.GetType() == type) 

Edit: As Luke explained in the comments, the โ€œoriginal typeโ€ is actually the original type when provided by RuntimeType - it doesn't work for anything else. And since this type is internal to the system, the method seems simply wrong.

+2
source

All Articles