Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.GetType() source code

Tags:

.net

How is object.GetType() implemented in .NET?

like image 333
Glebka Avatar asked Feb 20 '11 11:02

Glebka


People also ask

What is GetType() C#?

C# String GetType() The C# GetType() method is used to get type of current object. It returns the instance of Type class which is used for reflection.

How to check the value type in C#?

The GetType() method of array class in C# gets the Type of the current instance. To get the type. Type tp = value. GetType();


1 Answers

It's implemented in the runtime itself, so there is no C# source-code for it.

[MethodImpl(MethodImplOptions.InternalCall)]
public extern Type GetType();

MethodImplOptions.InternalCall is used for functions which have a "magical" implementation inside the runtime itself.

For the normal .net implementation you won't find it at all since its closed source. With Rotor or Mono you'll most likely find in their c/c++ runtime source-code.

I assume it just uses the marker pointer at the beginning of each instance to get to the class information which then contains a field to get to the managed Type instance, possibly creating it on demand.

like image 64
CodesInChaos Avatar answered Nov 28 '22 14:11

CodesInChaos