Deserializing an object from another assmebly?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am in the process of re-writing our plugin, which means a whole new assembly. I am trying to maintain backwards compatability with our old plugin for when the time comes that we switch over.
The problem I'm running into is some data was written to an XRecord in the form of a List of objects from the old assembly. So the old assembly has an interface IZone, and the XRecord data is a List<IZone> that was serialized in this way
public static ResultBuffer SerializeXrecordData(object objectToSerialize)
{
ResultBuffer buffer;
MemoryStream serializationStream = new MemoryStream();
try
{
new BinaryFormatter().Serialize(serializationStream, objectToSerialize);
byte[][] bufferArray = ChunkStream(serializationStream);
if (bufferArray.Length <= 0)
{
buffer = null;
}
else
{
List<TypedValue> list = new List<TypedValue> {
new TypedValue(1, objectToSerialize.GetType().FullName)
};
int index = 0;
while (true)
{
if (index >= bufferArray.Length)
{
buffer = new ResultBuffer(list.ToArray());
break;
}
list.Add(new TypedValue(310, bufferArray[index]));
index++;
}
}
}
finally
{
if (!ReferenceEquals(serializationStream, null))
{
serializationStream.Dispose();
}
}
return buffer;
}
public static byte[][] ChunkStream(MemoryStream stream)
{
byte[][] bufferArray = new byte[(int) Math.Ceiling((decimal) (stream.Length / 127M))][];
stream.Position = 0L;
int index = 0;
int length = (int) stream.Length;
int num4 = 0;
while (true)
{
if (length <= 0)
{
break;
}
int count = 0x7f;
if (count > length)
{
count = length;
}
byte[] buffer = new byte[count];
int num6 = stream.Read(buffer, 0, count);
if (num6 == 0)
{
break;
}
bufferArray[index] = buffer;
index++;
length -= num6;
num4 += num6;
}
return bufferArray;
}
I have made a copy of the C# file for the IZone Interface in my new project so that I would have the type.
And when I try to deserialize and casting to List<IZone> using the deserialization method from the old assembly I get an error saying
Object of Type [CompanyName]Tools.Interfaces.IZone[] cannot be cast to type [CompanyName]Tools.Interfaces.IZone[]
This is how the deserialization occurs
public static object DeSerializeXrecordData(ResultBuffer xrecordData)
{
object obj2;
bool flag = xrecordData != null;
if (!flag)
{
obj2 = null;
}
else
{
BinaryFormatter formatter = new BinaryFormatter {
Binder = new DomainBinder()
};
MemoryStream serializationStream = new MemoryStream();
try
{
TypedValue[] valueArray = xrecordData.AsArray();
int index = 1;
while (true)
{
flag = index < valueArray.Length;
if (!flag)
{
serializationStream.Position = 0L;
obj2 = formatter.Deserialize(serializationStream);
break;
}
if (valueArray[index].TypeCode == 310)
{
byte[] buffer = (byte[]) valueArray[index].Value;
serializationStream.Write(buffer, 0, buffer.Length);
}
index++;
}
}
finally
{
if (!ReferenceEquals(serializationStream, null))
{
serializationStream.Dispose();
}
}
}
return obj2;
}
public class DomainBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
string str = assemblyName.Split(new char[] { ',' })[0];
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
int index = 0;
while (true)
{
Type type;
if (index >= assemblies.Length)
{
type = null;
}
else
{
char[] separator = new char[] { ',' };
if (str != assemblies[index].FullName.Split(separator)[0])
{
index++;
continue;
}
type = assemblies[index].GetType(typeName);
}
return type;
}
}
}
I know this is a weird/hard one, but any help would be greatly appreciated