Testing if ValueType is default, unassigned value

Hi,

I am having some trouble finding the default, unassigned value of ValueType properties using reflection.

What I am trying to do is write some code that automatically set some properties, but only if the initial property value is in it's default, unassigned state. For property reference types it is easy and I am just checking if the property is null, but even after searching the reference and google I am no closer to finding out to get the default value of a ValueType to test against.

Any help would be appreciated.

[667 byte] By [JamesNewton-King] at [2008-1-23]
# 1
James Newton-King wrote:

I am having some trouble finding the default, unassigned value of ValueType properties using reflection.


You could use reflection to invoke the value type's default constructor and so create an instance of the value type with the default unassigned value.

FrankBoyne at 2007-8-21 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 2
Yeah, I tried doing that after posting and it does work.

Shame there isn't a cleaner method. I would have thought that sort of information would be on the Type class.

JamesNewton-King at 2007-8-21 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 3
James Newton-King wrote:
Shame there isn't a cleaner method.
Perhaps this sort of problem is what Nullable Types were introduced to handle (in CLR 2.0)?

http://msdn2.microsoft.com/library/1t3y8s4s(en-us,vs.80).aspx

FrankBoyne at 2007-8-21 > top of Msdn Tech,.NET Development,Common Language Runtime...

.NET Development

Site Classified