I am curious what the "method body" for typeof in C# would look like (pretty sure I can't get to it in reflector as it's a keyword not a method).
I am guessing it is equivalent to GetType(magic convert symbol to string).
Looking at GetType(string) in Reflector it calls a method "PrivateGetType", which calls "RuntimeTypeHandle.GetTypeByName", and RuntimeTypeHandle seems to have a lot of the logic behind types in it, but the GetTypeByName stuff doesn't show up in Reflector.
The typeof operator is not a variable. It is an operator. Operators ( + - * / ) do not have any data type. But, the typeof operator always returns a string (containing the type of the operand).
The typeof operator returns a string indicating the type of the operand's value.
The typeof operator takes only one operand (a unary operator). It evaluates the type of the operand and returns the result as a string. Here is how you use it when you're evaluating the type of a number, 007. In the above example, the expression typeof 007 evaluates to the type number and returns the string 'number'.
Below is the example of the typeof operator. In JavaScript, the typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable.
If you do something like:
Type t = typeof(string);
Then the compiler compiles the typeof(string)
bit to a ldtoken MSIL instruction and then calls Type.GetTypeFromHandle to get an instance of the Type
class.
Type.GetTypeFromHandle
is implemented by the runtime (which is why it's marked with the "MethodImplOptions.InternalCall" attribute). You can look at the source code to mono for how it's actually implemented, but you basically have to understand the whole metadata system to understand how Type
and friends works internally...
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