Link to home
Start Free TrialLog in
Avatar of tacojoosterkamp
tacojoosterkamp

asked on

Use Reflection to determine if a property is System.Nullable<System.DateTime>

Hi,

I'm trying to use reflection to copy some Request.QueryString[] values into a Linq2SQL object. This I do by iterating through the properties of the Linq Object and then checking if there is a QueryString available for that property.

However, these properties have different kind of types and I need to convert the QueryString data to the appropriate type before I can store it in the properties of the Linq Object.

This seems to work well:
Type propertyType = propertyInfo.GetType();
if (propertyType == typeof(Int32))
{
 // do some intelligent stuff here
}

But the following one does not work if the column is marked as Nullable:

if (propertyType == typeof(System.Nullable<System.DateTime>))
{
 // do some intelligent stuff here
}

Any suggestions as to how to fix this?
Taco
ASKER CERTIFIED SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
even if u declare datetime as nullable either
Nullable<DateTime> val = DateTime.Now;
or
DateTime ? val = DateTime.Now;

and check its type u get "DateTime"
check this code snippet:

Nullable<DateTime> val = DateTime.Now;
            Type t = val.GetType();
            bool equal = (t == typeof(DateTime));

equal is always true...