
Signature
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
There is a problem with this (and the OP's solution) when it comes to
nullable types.
Assuming the value is in an object (if it was typed, then there would be
no reason to check for it in the first place, since you can always do
"typeof(KnownType).IsValueType"), and the value from a Nullable<T> which was
null was stored to it, you would end up with an object reference of null,
with no way of calling GetType. When boxing the nullable value, it actually
puts null (not a Nullable<T> with a value of null) in the object reference.
There really is no good way of doing this if you are using Nullable
values along with an object reference and want to call GetType on that
variable.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hi,
>
[quoted text clipped - 5 lines]
>> for reference type, I can use the is keyword, but how can test
>> whether a specific type is a value type?
Marc Gravell - 02 Jan 2008 22:51 GMT
> There really is no good way of doing this if you are using Nullable
> values along with an object reference and want to call GetType on that
> variable.
Absolutely true - but generics (with type inference) can help a little
bit - i.e. if you have SomeMethod<T>(T whatever) then the caller can
use SomeMethod(localTypedValue), and then if needed you can use
typeof(T).IsValueType (or whatever). Again, however, you are
completely scuppered if you get down to "object"...
I guess the point I'm trying to make is that: in addition to "object"
as a parameter, generics *also* provide a mechanism for writing a
utility method dealing with an unpredictable object-type (normally
without changing the call syntax). However, to be effective (i.e.
typeof(T) to be useful), the calling method *must* know the actual
type [even if just another "T"] (not just "System.Object").
Marc
Nicholas Paldino [.NET/C# MVP] - 03 Jan 2008 15:25 GMT
Marc,
I thought of that, but omitted it because if the type was known at
compile time, the whole conversation is moot, but it is a valid point
(assuming you know the type at compile time).

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
>> There really is no good way of doing this if you are using Nullable
>> values along with an object reference and want to call GetType on that
[quoted text clipped - 13 lines]
>
> Marc