对于感兴趣的朋友,我搜索了很多,我想出了一个使用泛型和反射的解决方案:
public static class MyConvertingClass
{
public static T Convert<T>(APIElement element)
{
System.Type type = typeof(T);
if (conversions.ContainsKey(type))
return (T)conversions[type](element);
else
throw new FormatException();
}
private static readonly Dictionary<System.Type, Func<Element, object>> conversions = new Dictionary<Type,Func<Element,object>>
{
{ typeof(bool), n => n.GetValueAsBool() },
{ typeof(char), n => n.GetValueAsChar() },
{ typeof(DateTime), n => n.GetValueAsDatetime() },
{ typeof(float), n => n.GetValueAsFloat32() },
{ typeof(double), n => n.GetValueAsFloat64() },
{ typeof(int), n => n.GetValueAsInt32() },
{ typeof(long), n => n.GetValueAsInt64() },
{ typeof(string), n => n.GetValueAsString() }
};
}
public static main()
{
// Defined by the user:
Type fieldType = typeof(double);
// Using reflection:
MethodInfo method = typeof(MyConvertingClass).GetMethod("Convert");
method = method.MakeGenericMethod(fieldType);
Console.WriteLine(method.Invoke(null, new object[] { fieldData }));
}