Eatdoku
Eatdoku

Reputation: 6951

C# determine a Nullable property DateTime type when using reflection

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

ObjectA has a property DateTime? CreateDate;

When I iterate through its properties like the following code, how do I check if a property is a Nullable DateTime type?

foreach (PropertyInfo pi in ObjectA.GetType().GetProperties())
{
    //do the compare here
}

Upvotes: 25

Views: 26067

Answers (3)

Vinicius Grund
Vinicius Grund

Reputation: 21

Try:

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

Upvotes: 2

Manish Basantani
Manish Basantani

Reputation: 17509

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

Upvotes: 4

Pavel Minaev
Pavel Minaev

Reputation: 101605

pi.PropertyType == typeof(DateTime?)

Upvotes: 58

Related Questions