Ich versuche, ein Objektarray wich serialisiert enthält ein Wörterbuch als eine ihrer Werte serialisieren, und ich diese Laufzeit SerializationException bekommen:Wie Objektarray mit einem Wörterbuch innerhalb
Type 'System.Collections.Generic.Dictionary`2 ...with data contract name 'ArrayOfKeyValueOfstringanyType
is not expected. Consider using a DataContractResolver if you are using
DataContractSerializer or add any types not known statically to the list of
known types - for example, by using the KnownTypeAttribute attribute or by
adding them to the list of known types passed to the serializer.
Diese ist, wie ich die Aufgabe, ich versuche zu erreichen:
object[] taskArgs = new object[] { 1, 2 };
IDictionary<string, object> kwargs = new Dictionary<string, object>();
IDictionary<string, object> embed = new Dictionary<string, object>();
embed.Add("callbacks", null);
embed.Add("errbacks", null);
embed.Add("chain", null);
embed.Add("chord", null);
var knownTypes = new List<Type> { typeof(IDictionary<string, object>), typeof(object []), typeof(List<string>) };
//object[] arguments = new object[] { taskArgs, "{}", "{}" };
object[] arguments = new object[] { taskArgs, kwargs, embed };
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(object[]), knownTypes);
ser.WriteObject(stream1, arguments);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
string message = sr.ReadToEnd();
Auch wenn ich typeof(IDictionary<string, object>)
-knownTypes
hinzuzufügen habe versucht, es wird nicht funktionieren.
Jede Hilfe wird sehr geschätzt. Vielen Dank.
Danke! Das hat mir wirklich geholfen :) –