我正在使用SerializableDictionary
博客中的一个类,并且还使用一些复杂的类型进行序列化。不幸的是,我收到一个错误,不知道如何解决这个问题。
需要注意的一点是,我使用的是 Mono,因为我使用的是 Unity3D。我不知道这是否会使这个问题有所不同,而且我还没有测试 .net 是否也返回此错误。
这是堆栈跟踪的上半部分:
InvalidOperationException:参数对象 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' 的类型不是原始的。
System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(System.String 名称,System.String ns,System.Object o,布尔 xsiType)
namespace Foo
{
class Program
{
static void Main(string[] args)
{
Foo f = new Foo();
string result = f.Serialize();
Console.WriteLine(result);
Console.ReadLine();
}
}
class Foo
{
private SerializableDictionary<string, object> dict;
public Foo()
{
dict = new SerializableDictionary<string, object>();
dict.Add("list", new List<string>() { "test1", "test2" });
}
public string Serialize()
{
XmlSerializer x = new XmlSerializer(dict.GetType(), new Type[] {typeof(List<string>)});
StringWriter w = new StringWriter();
x.Serialize(w, dict);
return w.ToString();
}
}
}
更新:
我在 .NET 中创建了一个测试程序,它告诉我{"The type System.Collections.Generic.List`1[[System.String,...]] may not be used in this context." }。
我已经更新了上面的示例代码。