C# determine a Nullable property DateTime type whe

2019-04-19 06:44发布

问题:

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
        }

回答1:

pi.PropertyType == typeof(DateTime?)


回答2:

pi.PropertyType == typeof(Nullable<DateTime>);


回答3:

Try:

property.PropertyType.Equals(typeof(DateTime?))