c# - Why does one class fail to serialize when another succeeds, yet they have the same ancestor class? -


i have class called dataimportconfiguration contains reference class called datamapping , inheriting child class lookupdatamapping:

public class lookupdatamapping : datamapping {     #region properties      public lookupinfo lookupinfo { get; }      #endregion      #region constructors      public lookupdatamapping(string sourcename, string targetname, lookupinfo lookupinfo, string defaultvalue) :         base(sourcename, targetname, importprecision.optional, " ", defaultvalue)     {         lookupinfo = lookupinfo;     }      #endregion  } 

this serialized perfectly. today added new class, lookuplistdatamapping, has same ancestor:

public class lookuplistdatamapping : datamapping {      #region properties      public string[] lookuplist { get; }     public string sourcename { get; }      #endregion      #region constructors      public lookuplistdatamapping(string sourcename, string targetname, string[] lookuplist, string defaultvalue) :         base(sourcename, targetname, importprecision.optional, " ", defaultvalue)     {         sourcename = sourcename;         lookuplist = lookuplist;         targetname = targetname;     }      #endregion  } 

nothing different here, , no unknown types. yet, on serialization using datacontract serializer works other class this:

unable serialize aceartiste data.sai file

the system returned message:

type 'lookuplistdatamapping' data contract name 'lookuplistdatamapping:http://schemas.datacontract.org/2004/07/dataimportmodule' not expected. consider using datacontractresolver if using datacontractserializer or add types not known statically list of known types - example, using knowntypeattribute attribute or adding them list of known types passed serializer

if add knowntypeattribute of own type, serialize not deserialize. if use different type (e.g. string[] differing type) won't serialize.

can explain me why 1 works , 1 doesn't? if can tell me make work correctly, better :-)

edit

as requested, here ancestor class:

[serializable] [knowntype(typeof(lookuplistdatamapping))] public class datamapping {      #region private instance members      private readonly list<sourceattempt> _sourceattemptslist;      #endregion      #region properties      [datamember]     public string defaultvalue { get; set; }      public textformat textformat { get; set; }      [datamember]     public importprecision precision { get; set; }     [datamember]     public string separator { get; set; }      public sourceattempt[] sourceattempts => _sourceattemptslist.toarray();      [datamember]     public string targetname { get; set; }      #endregion      #region constructors      [obsolete("required serialization", true)]     public datamapping() { }      public datamapping(string sourcename, string targetname) : this(new[] { sourcename }, targetname, importprecision.optional, " ", string.empty) { }      public datamapping(string sourcename, string targetname, textformat textformat = textformat.none)         : this(new[] { sourcename }, targetname, importprecision.optional, " ", string.empty, textformat) { }      public datamapping(string sourcename, string targetname, importprecision importprecision, textformat textformat)         : this(new[] { sourcename }, targetname, importprecision, " ", string.empty, textformat) { }      public datamapping(string sourcename, string targetname, importprecision precision = importprecision.optional,         string separator = " ", string defaultvalue = "", textformat textformat = textformat.none) :         this(new[] { sourcename }, targetname, precision, separator, defaultvalue, textformat)     { }      public datamapping(string[] sourcenames, string targetname,         importprecision precision = importprecision.optional, string separator = " ", string defaultvalue = "", textformat textformat = textformat.none) :         this(new[] { new sourceattempt(sourcenames) }, targetname, precision, separator, defaultvalue, textformat)     { }      public datamapping(sourceattempt[] sourceattempts, string targetname,         importprecision precision = importprecision.optional, string separator = " ", string defaultvalue = "", textformat textformat = textformat.none)     {         _sourceattemptslist = new list<sourceattempt>();         _sourceattemptslist.addrange(sourceattempts);          defaultvalue = defaultvalue;         textformat = textformat;         precision = precision;         separator = separator;         targetname = targetname;     }      #endregion  } 

for avoidance of doubt, serializing class has collection of datamappings part of it, part of collection of class, part of class. lot of code if post all!! serialize entire ultimate ancestor class, , deserialize determine how import routine should function.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -