我试图写点什么,但我已经走到了尽头。我有以下代码:
public class ObjectConverter
{
private Dictionary<Type, object> m_Conversions = new Dictionary<Type, object>();
public void AddConversion<TOut>(Func<object, TOut> conversion)
{
m_Conversions[typeof(TOut)] = conversion;
}
public T Convert<T>(object value)
{
var conversion = (Func<object, T>) m_Conversions[typeof(T)];
return conversion(value);
}
}
它是对真实事物的简化,但基本上它允许将对象转换为我们已定义转换的任何类型。 这样你就可以做到这样的事情:
// Intialization
converter.AddConversion(x => Convert.ToInt32(x));
// Some other place
converter.Convert<int>("12");
到目前为止一直很好,但是它变得复杂的是我想写一个非泛型的转换版本,如此
object Convert(object value, Type type)
{
var conversion = m_Conversions[type];
// ???
}
我该怎么做?我想做类似的事情:
object Convert(object value, Type type)
{
var conversion = m_Conversions[type];
var funcType = typeof(Func<,>).MakeGenericType(typeof(object), type);
var invoke = funcType.GetMethod("Invoke");
return invoke.Invoke(conversion, new object[] { value });
}
但似乎效率很低。你能想到更好的方法吗?
答案 0 :(得分:0)
Func
is covariant,例如每个Func<object, string>
都会返回object
,因此可以安全地转换为Func<object, object>
。所以在你的情况下你根本不需要反思。
public class ObjectConverter
{
private Dictionary<Type, Func<object, object>> m_Conversions = new Dictionary<Type, Func<object, object>>();
public void AddConversion<TOut>(Func<object, TOut> conversion)
{
// need this to support TOut as value type.
m_Conversions[typeof(TOut)] = obj => conversion(obj);
}
public T Convert<T>(object value)
{
return (T) Convert(value, typeof(T));
}
object Convert(object value, Type type)
{
var conversion = m_Conversions[type];
return conversion(value);
}
}