0

GetType() is defined as extern in base Object. As far as I know, .NET source code (I use .NET 4.6) does not even provide the source code for any extern method. Does anybody know where the source code for GetType() is?

I am trying to learn the boxing of simple types and I use GetType() for various tests. Technically, when you run it on a literal/variable of simple type then the literal/variable is automatically boxed so what is the Type returned by GetType() supposed to represent? My tests show it is pre-boxed type which is problematic because GetType() is run on a boxed type. Even if I cast a variable of simple type into an object first, and run GetType() on this object itself then the returned Type still shows simple type. Also, if I declare a variable of nullable simple type like int? and run GetType() on it then Type.IsValueType = True and Type.IsPrimitive = True (IsPrimitive should be false). Only when I check Type returned by typeof(int?) I get Type.IsValueType = True and Type.IsPrimitive = False

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Micky6789
  • 43
  • 5
  • What do you mean by 'pre-boxed type'? A unboxed and boxed value have the same type. – Lee Apr 30 '17 at 16:26
  • 1
    Yes, but when you have a variable `int? n = ...;`, then, because of the strange boxing behavior of the `Nullable<>` type, the call `n.GetType()` will not return the true type `typeof(int?)`. It will either blow up with an exception (when `HasValue` property is False), or return just `typeof(int)` (when `HasValue` is True). – Jeppe Stig Nielsen Apr 30 '17 at 16:29
  • I'm not sure I'm understanding what you're talking about here. `typeof(int?)` and `typeof(Nullable)` resolve to the same type. `typeof(int?)` and `typeof(Nullable<>)` do not (one is open generic). – rossipedia Apr 30 '17 at 16:31
  • There are several issues here. First issue is the proper documentation of GetType(). After all you run it on an instance of an object and when it returns type that is not an object then we have a huge problem with basic logic. This is not a church where you follow a religion. You have to stick to a logic. Second issue is how to test a type during runtime. – Micky6789 Apr 30 '17 at 16:39
  • 2
    "extern" just means that the method is implemented by the CLR. Which is pretty common for such "works everywhere, does magic" methods, source is C++ and available through the CoreCLR project. The returned type has the expected value, the framework does not have a dedicated type that means "boxed value". It always provides you with the type of the value in the box. Magic. Nor do you need one, you already know because you are calling GetType() on a variable of type `object`. If you have "doesn't work" code then you need to show it. – Hans Passant Apr 30 '17 at 16:42
  • @rossipedia I agree with that. But boxing turns an `int?` into an `int` (in a box); or into a null reference (no box or other instance). When `n` has type `int?`, the call `n.GetType()` does not return `int?`. – Jeppe Stig Nielsen Apr 30 '17 at 17:18
  • You're right. That's pretty damn unexpected :-\ – rossipedia Apr 30 '17 at 17:33

0 Answers0