I wrote generic methods that use reflection to search an appropriate Parse method and call it. But they won't work if you want to transform string to string since string doesn't have a Parse method. So you'll need to add a special case for string.
I don't understand why your function is called GetDefaultValue either. Why not Parse, TryParse, ConvertFromString or something like that? When seeing a function called GetDefaultValue, I don't think of a parsing function.
Check this old question:
Is it possible to make a generic number parser in C#? which has several relevant answers.
And my old answer from there:
I have written some code that uses reflection to find Parse/TryParse methods on a type and access these from generic functions:
private static class ParseDelegateStore<T>
{
public static ParseDelegate<T> Parse;
public static TryParseDelegate<T> TryParse;
}
private delegate T ParseDelegate<T>(string s);
private delegate bool TryParseDelegate<T>(string s, out T result);
public static T Parse<T>(string s)
{
ParseDelegate<T> parse = ParseDelegateStore<T>.Parse;
if (parse == null)
{
parse = (ParseDelegate<T>)Delegate.CreateDelegate(typeof(ParseDelegate<T>), typeof(T), "Parse", true);
ParseDelegateStore<T>.Parse = parse;
}
return parse(s);
}
public static bool TryParse<T>(string s, out T result)
{
TryParseDelegate<T> tryParse = ParseDelegateStore<T>.TryParse;
if (tryParse == null)
{
tryParse = (TryParseDelegate<T>)Delegate.CreateDelegate(typeof(TryParseDelegate<T>), typeof(T), "TryParse", true);
ParseDelegateStore<T>.TryParse = tryParse;
}
return tryParse(s, out result);
}
https://github.com/CodesInChaos/ChaosUtil/blob/master/Chaos.Util/Conversion.cs
But I haven't tested them too much, so they might stiff have some bugs/not work correctly with every type. The error handling is a bit lacking too.
And they have no overloads for culture invariant parsing. So you probably need to add that.