使用反射,我试图抓住一个类字段并填充它们。目前我让它检测一个实例Dictionary<,>
并创建一个Dictionary<object,object>
来填充。之后它尝试更改类型,但这不起作用并且无法转换:
// Looping through properties. Info is this isntance.
// Check is a dictionary field.
Dictionary<object, object> newDictionary = new Dictionary<object, object>();
// Populating the dictionary here from file.
Type[] args = info.PropertyType.GetGenericArguments();
info.GetSetMethod().Invoke(data, new object[]
{
newDictionary.ToDictionary(k => Convert.ChangeType(k.Key, args[0]),
k => Convert.ChangeType(k.Value, args[1]))
});
有任何想法吗?谢谢。