C#反射字典
假设我有这个代码:C#反射字典,c#,serialization,reflection,dictionary,C#,Serialization,Reflection,Dictionary,假设我有这个代码: Dictionary<String, String> myDictionary = new Dictionary<String, String>(); Type[] arguments = myDictionary.GetType().GetGenericArguments(); Dictionary myDictionary=newdictionary(); Type[]arguments=myDictionary.GetType().GetGen
Dictionary<String, String> myDictionary = new Dictionary<String, String>();
Type[] arguments = myDictionary.GetType().GetGenericArguments();
Dictionary myDictionary=newdictionary();
Type[]arguments=myDictionary.GetType().GetGenericArguments();
在我的程序myDictionary中,它是未知类型的(它是从反序列化XML返回的对象),但就这个问题而言,它们是字符串。我想创建如下内容:
Dictionary<arguments[0],arguments[1]> mySecondDictionary = new Dictionary<arguments[0],arguments[1]>();
((reconstructedClass)returnedObject).lista
Dictionary mysecondictionary=newdictionary();
显然,它不起作用。
我在MSDN上搜索了一下,我看到他们正在使用Activator类,但我不明白。
也许更高级的人可以帮我一点忙。您可以使用前面提到的activator类,以便从给定类型创建对象。该方法允许您指定一个类型数组作为泛型对象的参数,这正是您试图模拟的
Dictionary<String, String> myDictionary = new Dictionary<String, String>();
Type[] arguments = myDictionary.GetType().GetGenericArguments();
Type dictToCreate = typeof(Dictionary<,>).MakeGenericType(arguments);
var mySecondDictionary = Activator.CreateInstance(dictToCreate);
Dictionary myDictionary=newdictionary();
Type[]arguments=myDictionary.GetType().GetGenericArguments();
Type dictToCreate=typeof(字典)。MakeGenericType(参数);
var mysecondictionary=Activator.CreateInstance(dictToCreate);
上面的代码基本上是毫无意义的,因为您知道字典是
String,String
,但是假设您在运行时有办法在其他地方检测所需的类型,您可以使用最后两行来实例化该类型的字典。这种方法存在问题。
我会尽力解释的。
我编写了一个程序,它首先将一个类序列化为XML,然后反序列化回来。
基本上,类是泛型的,它包含一个列表(与类的类型相同)。
因此,类的类型可以是任何类型,从简单类型开始,比如string、int等,到更复杂的类,比如book类或person。在使用XmlSerializer.Deserialize方法并获取对象之后,我应该使用反射来重构对象,并访问列表。我不能那样做。
所以,如果我有这样的东西:
Type classToCreate = typeof(classToBeSerialized<>).MakeGenericType(arguments);
var reconstructedClass = Activator.CreateInstance(classToCreate);
基本上,我使用反射将对象投射到它的源。我知道这是一个旧线程,但我只是需要类似的东西,并决定显示它(你知道谷歌) 这基本上是@user2536272对答案的重写
public object ConstructDictionary(Type KeyType, Type ValueType)
{
Type[] TemplateTypes = new Type[]{KeyType, ValueType};
Type DictionaryType = typeof(Dictionary<,>).MakeGenericType(TemplateTypes);
return Activator.CreateInstance(DictionaryType);
}
public void AddToDictionary(object DictionaryObject, object KeyObject, object ValueObject )
{
Type DictionaryType = DictionaryObject.GetType();
if (!(DictionaryType .IsGenericType && DictionaryType .GetGenericTypeDefinition() == typeof(Dictionary<,>)))
throw new Exception("sorry object is not a dictionary");
Type[] TemplateTypes = DictionaryType.GetGenericArguments();
var add = DictionaryType.GetMethod("Add", new[] { TemplateTypes[0], TemplateTypes[1] });
add.Invoke(DictionaryObject, new object[] { KeyObject, ValueObject });
}
公共对象构造字典(类型KeyType,类型ValueType)
{
类型[]TemplateTypes=新类型[]{KeyType,ValueType};
类型DictionaryType=typeof(Dictionary.MakeGenericType(TemplateTypes);
返回Activator.CreateInstance(DictionaryType);
}
public void AddToDictionary(对象字典对象、对象键对象、对象值对象)
{
类型DictionaryType=DictionaryObject.GetType();
if(!(DictionaryType.IsGenericType&&DictionaryType.GetGenericTypeDefinition()==typeof(Dictionary)))
抛出新异常(“对不起,对象不是字典”);
类型[]TemplateTypes=DictionaryType.GetGenericaArguments();
var add=DictionaryType.GetMethod(“add”,new[]{TemplateTypes[0],TemplateTypes[1]});
调用(DictionaryObject,新对象[]{KeyObject,ValueObject});
}
请给您的问题一个有意义的标题。不要只列出标签。这毫无意义,也不会吸引用户来调查和尝试帮助你。只需记下你最近删除的问题。