I have a method that is defined like such:
public bool IsValid(string propertyName, object propertyValue)
{
bool isValid = true;
// Validate property based on type here
return isValid;
}
I would like to do something like:
if (propertyValue is bool?)
{
// Ensure that the property is true
}
My challenge is, I’m not sure how to detect whether my propertyValue is a nullable bool or not. Can someone tell me how to do this?
Thank you!
The value of
propertyValuecould never be aNullable<bool>. As the type ofpropertyValueisobject, any value types will be boxed… and if you box a nullable value type value, it becomes either a null reference, or the boxed value of the underlying non-nullable type.In other words, you’d need to find the type without relying on the value… if you can give us more context for what you’re trying to achieve, we may be able to help you more.