C# determine a Nullable property DateTime type when using reflection

核能气质少年 提交于 2019-12-06 19:03:01

问题


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
}

回答1:


pi.PropertyType == typeof(DateTime?)



回答2:


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



回答3:


Try:

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


来源:https://stackoverflow.com/questions/1180730/c-sharp-determine-a-nullable-property-datetime-type-when-using-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!