views:

1497

answers:

2

Hi,

I have a question on how to determine an object's Nullable property type.

ObjectA with a property DateTime? CreateDate;

when i am iterate through it's properties like the following code, how do I check if a property is a Nullable DateTime type?

thanks

foreach (PropertyInfo pi in ObjectA.GetType().GetProperties())
        {
            //do the compare here
        }
+9  A: 
pi.PropertyType == typeof(DateTime?)
Pavel Minaev
thank you~....:)
Eatdoku
another question thou...how do I do switch based on the type? do i have to use fullname instead? or should be using "If" statement instead?what's the string FullName for a Nullable DateTime type?thank you
Eatdoku
I would strongly advise you to use `if`, and avoid `FullName`. If you want to see `FullName` for `DateTime?`, then just print out `typeof(DateTime?).FullName` - but it's going to be lengthy, will make your code less readable, be brittle (what if you occasionally delete a character somewhere?), and will result in slower comparisons (`Type` objects themselves are compared by reference - i.e. there's at most one `Type` object for any given, so if two references are equal, then this is the same type; and such comparison is fast)
Pavel Minaev
got it, thanks for the advise.
Eatdoku
+2  A: 
pi.PropertyType == typeof(Nullable<DateTime>);
Amby