I have to validate user input data and ensure a string value is convertible to a type specified at run-time. I don't necessarily need to do the actual conversion, just test to make sure the input value is valid. I haven't found a built in class or method that will perform this type of evaluation, but if I am missing one, please let me know. I'm working with C#4.0, if there is any version specific solutions available.
The method only has to deal with the "standard" types (built-in value data types plus String). The only custom type I would need to evaluate is specific enum types that are defined in the library.
I have 2 solutions I'm currently weighing, but neither is perfect, so I was hoping there was a 3rd option (or something built into the framework that I missed). I am heavily leaning towards Solution #2 since using the try-catch in Solution #1 just seems wrong.
Solution 1: Convert.ChangeType()
with try/catch
public Boolean CheckType(String value, Type type)
{
try
{
var obj = Convert.ChangeType(value, type);
return true;
}
catch(InvalidCastException)
{
return false;
}
catch(FormatException)
{
return false;
}
catch(OverflowException)
{
return false;
}
catch(ArgumentNullException)
{
return false;
}
}
Solution 2 if/else chain with Type check and TryParse
public Boolean CheckType(String value, Type type)
{
if (type == typeof(String))
{
return true;
}
else if (type == typeof(Boolean))
{
Boolean b;
return Boolean.TryParse(value, out b);
}
else if (type == typeof(Int32))
{
Int32 i;
return Int32.TryParse(value, out i);
}
else if (type == typeof(Int64))
{
Int64 l;
return Int64.TryParse(value, out l);
}
// similar code to check all other types
// (Int16, UInt32, UInt64, UInt16, Byte, SByte, Single, Double, Decimal,
// Enum, Char, DateTime)
.
.
.
.
.
else
throw new ArgumentException("Invalid type evaluation");
}
This method may be called several hundred or even a thousand times in a short interval if the input data is seriously messed up or corrupted, so I'm worried that the repeated if/else checks will be a drag on performance (I'm not necessarily trying to optimize at this point, I just want to make sure I'm considering other options).
The other issue I have with both solutions is that both actually convert the string value to a new value of the expected type, and in both cases, I'm swallowing the result.
I found a better solution than either of my initial ideas in another question that was recently asked.
parapura rajkumar was on the right track with the TypeConverter class, but the required exception handling for the CanConvertFrom
method for non-exceptional events was what I was trying to avoid.
The TypeConverter.IsValid
method solved my problem, although it is not ideal because the IsValid method is just a wrapper for the CanConvertFrom
method and the required exception handling.
private Boolean CanCovert(String value, Type type)
{
TypeConverter converter = TypeDescriptor.GetConverter(type);
return converter.IsValid(value);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With