c# - How to serialize list property of a dynamic object -
i have issue serialization wcf service (json output). use dynamicobject return ligth json rest service.
this code return empty result (impossible serialize):
public dynamicjsonobject dowork() { dynamic result = new dynamicjsonobject(); result.values = new list<int>() { 1, 2 }; }
but code works perfectly
public dynamicjsonobject dowork() { dynamic result = new dynamicjsonobject(); result.values = 1; }
my dynamicjsonobject class :
[serializable] public class dynamicjsonobject : dynamicobject, iserializable { private idictionary<string, object> dictionary { get; set; } public dynamicjsonobject() { dictionary = new dictionary<string, object>(); } public dynamicjsonobject(serializationinfo info, streamingcontext context) { dictionary = new dictionary<string, object>(); } public override bool trygetmember(getmemberbinder binder, out object result) { var haskey = dictionary.containskey(binder.name); result = haskey ? dictionary[binder.name] : null; return haskey; } public override bool trysetmember(setmemberbinder binder, object value) { dictionary[binder.name] = value; return true; } public void getobjectdata(serializationinfo info, streamingcontext context) { foreach (string key in dictionary.keys) { info.addvalue(key.tostring(), dictionary[key]); } } }
so got error error 324 (net::err_empty_response) instead of json result {values: [1,2]}
i found solution. should declare manualy list of serializable.
in exemple, can add attribute knowntype on result object
[serializable] [knowntype(typeof(list<int>))] public class dynamicjsonobject : dynamicobject, iserializable { ... }
the other solution use serviceknowntype on wcf service class
[servicecontract] [serviceknowntype(typeof(list<int>))] public interface idataservice { ... }
for information, can use generic attribute knowntype(typeof(list)
Comments
Post a Comment