Type t = typeof(SerializableDictionary<,>);
Type[] typeArgs = { keyType, valueType};
Type constructed = t.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed);http://msdn.microsoft.com/zh-cn/library/system.type.makegenerictype.aspx
Type[] typeArgs = { keyType, valueType};
Type constructed = t.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed);http://msdn.microsoft.com/zh-cn/library/system.type.makegenerictype.aspx
可能我没有说清楚。在我取得了这个实例后,还要向这个实例中加入键值对,而这些键值对是从另一个具有相同泛型的Dictonary来的。Dictionary<int, string> d = ... //这里是获取了另外的一个DictionaryType t = typeof(SerializableDictionary<,>);
Type[] typeArgs = { keyType, valueType};
Type constructed = t.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed);
foreach (KeyValuePair<int, string> kvp in r) // 当然这里的int, string 也是不确定的。 KeyValuePair<?,?>
{
o.Add(kvp.Key, kvp.Value); // 这里的object类型不能用add方法
}怎么才能给这个反射生成的实例加入东西啊。 这里SerializableDictionary是Dictioanry的一个子类,自定义的。
可能我没有说清楚。在我取得了这个实例后,还要向这个实例中加入键值对,而这些键值对是从另一个具有相同泛型的Dictonary来的。Dictionary<int, string> d = ... //这里是获取了另外的一个DictionaryType t = typeof(SerializableDictionary<,>);
Type[] typeArgs = { keyType, valueType};
Type constructed = t.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed);
foreach (KeyValuePair<int, string> kvp in r) // 当然这里的int, string 也是不确定的。 KeyValuePair<?,?>
{
o.Add(kvp.Key, kvp.Value); // 这里的object类型不能用add方法
}怎么才能给这个反射生成的实例加入东西啊。 这里SerializableDictionary是Dictioanry的一个子类,自定义的。
把生成的dictionary用dynamic来表示的
dynamic o = Activator.CreateInstance(constructed);
foreach (KeyValuePair<int, string> kvp in d) // 当然这里的int, string 也是不确定的。 KeyValuePair<?,?>
{
o.Add(kvp.Key, kvp.Value); // 这里的object类型不能用add方法
}