I have this function I have to implement:
protected override ValidationResult IsValid(
Object value,
ValidationContext validationContext
)
{
//Here is where I wanna test whether the following conversion is applicable
var x = Convert.ToInt64(value);
}
I could wrap that line in a try-catch block, or with other ways to perform that test, here is one of them:
var convertible = value as IConvertible;
if (convertible != null)
var x = convertible.ToInt64(null);
What’s the most efficient way to do this?
here you can define a default value, if the parse (conversion) is applicable it will return the converted int64 otherwise the default value will be returned:
ie:
DefaultValue will be 1234
DefaultValue will be 0
You can also make it even shrorter by declaring the variable like this: