I am writing a basic model validator that uses reflection with a custom "Required" attribute. When a model is passed into the validator method, all properties are checked for this required attribute. Complex types are recursively validated as well. When a property is found with this attribute, it will either compare the value to null if it is a nullable type, else it will compare to default.
I'm having issues when comparing non-nullable type to default. The following condition is evaluating to false for type int, but it should be true:
if (prop.GetValue(testObject) == default)
{
Console.WriteLine($"{VALIDATION_FAILED_MESSAGE}: {baseObjectName}{testObject.GetType().Name}.{prop.Name} is null.");
isNullorDefault = true;
}
I see in the debugger that the value being compared is without a doubt the default value -> default(int) == 0, but it is evaluating to false. Why?
Aucun commentaire:
Enregistrer un commentaire