我试图返回一个数组,dictionary <string, object>
其中对象可能包含基本类型,如 int、bool 等,或者它可能包含另一个数组dictionary<string, object>
虽然我可以让它很好地序列化,但如果字典中有字典,它就不会反序列化。
我收到以下错误:
Error in line 1 position 543. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data from a type that maps to the name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfArrayOfKeyValueOfstringanyType'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'ArrayOfArrayOfKeyValueOfstringanyType' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.
班上:
[DataContract(Namespace = "CISICPD")]
[KnownType(typeof(Dictionary<string,object>))]
public class TestResponse
{
[DataMember]
public Dictionary<string,object>[] Results;
}
功能:
public TestResponse test(string test1, string test2)
{
TestResponse r = new TestResponse();
r.Results = new Dictionary<string, object>[1];
r.Results[0] = new Dictionary<string, object>();
r.Results[0].Add("field1", 26);
Dictionary<string, object>[] d = new Dictionary<string, object>[1];
d[0] = new Dictionary<string, object>();
d[0].Add("inner", 28);
r.Results[0].Add("dictionary", d);
return r;
}
运行它会给出错误消息,但我认为我得到了正确的 knowntype?
CISICPD.CPDClient t = new CISICPD.CPDClient();
CISICPD.TestResponse response = t.test("dgdf", "dfsdfd");