c# - Binary Deserializing implementations of an abstract class -


I have an abstract class that I am trying to serialize and delete a solid implementation. I have this in my abstract base class:

  [DataContract] Public class MyAbstractBase {[DataMember] Public string foo {get; Set; } // Some other abstractions that are meant to implement derivative classes}   

And in that class I add a method to the serialize:

  Serialize the public string serializase 64 () {// one base 64 string byte [] bytes; Long length = 0; MemoryStream YS = new memorystream (); Data Contrectorcierizer serializer = New Data Contributoriserizer (This .gateType ()); XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter (YS); Serializer Right object (binaryAdshire, this); BinaryDictionaryWriter.Flush (); Length = ws.Length; Bytes = ws.GetBuffer (); String encoded data = bytes. Long + ":" + Convert. Tobase 64 string (bytes, 0, bytes lang, base 64 formatting option. Non); Return encoded data; }   

It works fine, "something" is generated in it and does not really make any errors.

Of course, the problem comes with deserialization I added this to:

  public static MyAbstractBase DeserializeBase64 {strings} {int p = s.IndexOf (':'); Int length = convert toInt32 (s.Substring (0, P)); // Remove data from base 64 string! Byte [] MemoryData = Convert.Fombabs 64 String (S.Abstring (P + 1)); Memorystream rs = new memorystream (memory data, 0, length); DataContractSerializer serializer = New DataContractSerializer (typeAb (MyAbstractBase), new type & lt; type & gt; () {typeof (SomeOtherClass.MyDerivedClass)}); XmlDictionaryReader binaryDictionaryReader = XmlDictionaryReader.CreateBinaryReader (RS, XmlDictionaryReaderQuotas.Max); Returns (MyArabBSBase) Serializer Readobject (binaryplayerider); }   

I considered by adding "the known type" to my DataContractSerializer , it would be able to detect the derived class deserializing, but it appears It is not that it is complaining with the error:

Expected element 'MyAbstractBase' from the namespace. '' SomeOtherClass.MyDerivedClass' with the name 'element', name space '' .

So am I missing here?

I have a simple display of the problem on a net net bedel here:

Unfortunately, it will not run right there because the system is not included in the . Order Serialization Assemblies but if you leave it in a Visual Studio project, it will do the serial properly, but it stays on discretionary.

When serializing your data, use the same overloaded method for sequentiality as you want to alphabetically Use:

  DataContractSerializer serializer = New DataContractSerializer (typeAb (MyAbstractBase), new type & lt; type & gt; () {typeof (SomeOtherClass .MyDerivedClass)}); Furthermore, declare a KnownType attribute around its base class so it knows whether possible derivative classes can be deserialize it:  
  [ DataContract [KnownType (typeOf (SomeOtherclass.MyDerivedClass))] Public Category MyAbstractBase {[DataMember] Public String Foo {get; Set; } // Some other abstract methods that apply to derivative classes}    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -