c# - DataContractJsonSerializer and List<Interface> -


in code have serialize list<imodel> imodel interface concrete class model

here pseudo code them:

public interface imodel {     string codice { get; set; }     int position { get; } }  [datacontract] public class model : imodel {     public model(string codice, int position)     {         this.codice = codice;         this.position = position;      }     [datamember(name = "codice")]     public string codice { get; set; }     [datamember(name = "position")]     int position;     public int position { { return position; } } } 

after reading this post wrote:

datacontractjsonserializer jsonserializer = new datacontractjsonserializer(typeof(list<imodel>), new[] { typeof(model) }); using (filestream writer = file.create(@"c:\temp\modello.json"))         {             jsonserializer.writeobject(writer, mylist);         } 

it works ugly , output contains field type of element, "__type":"model:#someprojectname". there way serialize list when declared list<interfacename> while contains elements of concrete class implement interface ? tried casts got compilation error or runtime exceptions.

i specify in previous implementation copied items list<imodel> list<model> type known datacontractjsonserializer. in fact i'm sure imodel model.

why need interface such implementation? suggest generate c# classes json through http://json2csharp.com/. once done , paste classes , find <rootobject> class , serialize using code mentioned in question


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -