Author Topic: Problem deserializing an XRecord that was serialized by another plugin  (Read 1413 times)

0 Members and 1 Guest are viewing this topic.

shupsta2010

  • Mosquito
  • Posts: 17
I am in the process of re-writing our plugin for AutoCAD which is in C# using .NET

In the old plugin there were two classes, one is an interface IZone, and the other an instantiation of that interface Zone. I then had a List<IZone>, that was then serialized in this way

Code - C#: [Select]
  1. public static ResultBuffer SerializeXrecordData(object objectToSerialize)
  2.         {
  3.             ResultBuffer buffer;
  4.             MemoryStream serializationStream = new MemoryStream();
  5.             try
  6.             {
  7.                 new BinaryFormatter().Serialize(serializationStream, objectToSerialize);
  8.                 byte[][] bufferArray = ChunkStream(serializationStream);
  9.                 if (bufferArray.Length <= 0)
  10.                 {
  11.                     buffer = null;
  12.                 }
  13.                 else
  14.                 {
  15.                     List<TypedValue> list = new List<TypedValue> {
  16.                         new TypedValue(1, objectToSerialize.GetType().FullName)
  17.                     };
  18.                     int index = 0;
  19.                     while (true)
  20.                     {
  21.                         if (index >= bufferArray.Length)
  22.                         {
  23.                             buffer = new ResultBuffer(list.ToArray());
  24.                             break;
  25.                         }
  26.                         list.Add(new TypedValue(310, bufferArray[index]));
  27.                         index++;
  28.                     }
  29.                 }
  30.             }
  31.             finally
  32.             {
  33.                 if (!ReferenceEquals(serializationStream, null))
  34.                 {
  35.                     serializationStream.Dispose();
  36.                 }
  37.             }
  38.             return buffer;
  39.         }

I have made a copy of the IZone class and put it in my new plugin in the hopes to use it for the translation to the new method I am using for Serialization. But when I try to deserialize I get an error that I cant cast CompanyName.Interface.IZone to type CompanyName.Interface.IZone?

Code - C#: [Select]
  1. public static object DeSerializeXrecordData(ResultBuffer xrecordData)
  2.         {
  3.             object obj2;
  4.             bool flag = xrecordData != null;
  5.             if (!flag)
  6.             {
  7.                 obj2 = null;
  8.             }
  9.             else
  10.             {
  11.                 BinaryFormatter formatter = new BinaryFormatter {
  12.                     Binder = new DomainBinder()
  13.                 };
  14.                 MemoryStream serializationStream = new MemoryStream();
  15.                 try
  16.                 {
  17.                     TypedValue[] valueArray = xrecordData.AsArray();
  18.                     int index = 1;
  19.                     while (true)
  20.                     {
  21.                         flag = index < valueArray.Length;
  22.                         if (!flag)
  23.                         {
  24.                             serializationStream.Position = 0L;
  25.                             obj2 = formatter.Deserialize(serializationStream);
  26.                             break;
  27.                         }
  28.                         if (valueArray[index].TypeCode == 310)
  29.                         {
  30.                             byte[] buffer = (byte[]) valueArray[index].Value;
  31.                             serializationStream.Write(buffer, 0, buffer.Length);
  32.                         }
  33.                         index++;
  34.                     }
  35.                 }
  36.                 finally
  37.                 {
  38.                     if (!ReferenceEquals(serializationStream, null))
  39.                     {
  40.                         serializationStream.Dispose();
  41.                     }
  42.                 }
  43.             }
  44.             return obj2;
  45.         }

Code - C#: [Select]
  1. public class DomainBinder : SerializationBinder
  2.         {
  3.             public override Type BindToType(string assemblyName, string typeName)
  4.             {
  5.                 string str = assemblyName.Split(new char[] { ',' })[0];
  6.                 Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  7.                 int index = 0;
  8.                 while (true)
  9.                 {
  10.                     Type type;
  11.                     if (index >= assemblies.Length)
  12.                     {
  13.                         type = null;
  14.                     }
  15.                     else
  16.                     {
  17.                         char[] separator = new char[] { ',' };
  18.                         if (str != assemblies[index].FullName.Split(separator)[0])
  19.                         {
  20.                             index++;
  21.                             continue;
  22.                         }
  23.                         type = assemblies[index].GetType(typeName);
  24.                     }
  25.                     return type;
  26.                 }
  27.             }
  28.         }

I have tried this for the DomainBinder with still no luck

Code - C#: [Select]
  1. string str = assemblyName.Split(new char[] { ',' })[0];
  2.  
  3.             Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  4.  
  5.             if (str.Equals("[CompanyName]"))
  6.             {
  7.                 return Assembly.GetExecutingAssembly().GetType(typeName);
  8.             }

Sorry if you have seen this somewhere else, I'm just pretty desperate to get this to work for backwards compatability.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Problem deserializing an XRecord that was serialized by another plugin
« Reply #2 on: January 05, 2022, 06:19:00 PM »
I didn’t run your code, the only thing I see glaring is on the serialize side, DxfCode.BinaryChunk  (310) has a max size, I think it’s like 127 bytes.
So, SerializeXrecordData has undefined behavior with chunks lager.. I could be wrong though