C# 使用反射时确定一个 Nullable 属性 DateTime 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1180730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:12:03  来源:igfitidea点击:

C# determine a Nullable property DateTime type when using reflection

c#reflectionnullable

提问by Eatdoku

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

我有一个关于如何确定对象的 Nullable 属性类型的问题。

ObjectAhas a property DateTime? CreateDate;

ObjectA有财产 DateTime? CreateDate;

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

当我像下面的代码一样遍历它的属性时,我如何检查一个属性是否是一个Nullable DateTime类型?

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

采纳答案by Pavel Minaev

pi.PropertyType == typeof(DateTime?)

回答by Manish Basantani

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

回答by Vinicius Grund

Try:

尝试:

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