gpt4 book ai didi

c# - 在 C# 中检测可空类型

转载 作者:太空狗 更新时间:2023-10-29 20:01:53 25 4
gpt4 key购买 nike

我有一个这样定义的方法:

public bool IsValid(string propertyName, object propertyValue)
{
bool isValid = true;
// Validate property based on type here
return isValid;
}

我想做这样的事情:

if (propertyValue is bool?)
{
// Ensure that the property is true
}

我的挑战是,我不确定如何检测我的 propertyValue 是否为可为 null 的 bool 值。谁能告诉我该怎么做?

谢谢!

最佳答案

propertyValue 的值可能永远Nullable<bool> .作为 propertyValue 的类型是object ,任何值类型都将被装箱...如果您装箱一个可为 null 的值类型值,它将变为空引用或基础不可为 null 类型的装箱值。

换句话说,您需要在不依赖的情况下找到类型...如果您能为我们提供更多有关您要实现的目标的上下文,我们也许能够为您提供更多帮助。

关于c# - 在 C# 中检测可空类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6331246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com