Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

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
0
tacojoosterkamp
Asked:
tacojoosterkamp
1 Solution
 
p_davisCommented:
if(propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)
0
 
Meir RivkinFull stack Software EngineerCommented:
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...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now