How to call TryParse dynamically?
.net, c#, type-conversion
Solution
You could call the `TryParse` method dynamically using Reflection. This way you won't get a time consuming Exception if the conversion fails.
This method is a slightly optimized version of this one.
//Try Parse using Reflection
public static bool TryConvertValue<T>(string stringValue, out T convertedValue)
{
var targetType = typeof(T);
if (targetType == typeof(string))
{
convertedValue = (T)Convert.ChangeType(stringValue, typeof(T));
return true;
}
var nullableType = targetType.IsGenericType &&
targetType.GetGenericTypeDefinition() == typeof (Nullable<>);
if (nullableType)
{
if (string.IsNullOrEmpty(stringValue))
{
convertedValue = default(T);
return true;
}
targetType = new NullableConverter(targetType).UnderlyingType;
}
Type[] argTypes = { typeof(string), targetType.MakeByRefType() };
var tryParseMethodInfo = targetType.GetMethod("TryParse", argTypes);
if (tryParseMethodInfo == null)
{
convertedValue = default(T);
return false;
}
object[] args = { stringValue, null };
var successfulParse = (bool)tryParseMethodInfo.Invoke(null, args);
if (!successfulParse)
{
convertedValue = default(T);
return false;
}
convertedValue = (T)args[1];
return true;
}
Problem
Is there a way to call `TryParse` dynamically? Some kind of: ``` public static bool TryParse<T>(string toConvert, out T result) ``` Of course one can use Typeonverters for this. However, an invalid conversion will result in an exception and I want to get rid of this.