How is object.GetType()
implemented in .NET?
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.
The GetType() method of array class in C# gets the Type of the current instance. To get the type. Type tp = value. GetType();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With