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
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;
}
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?
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 have tried this for the DomainBinder with still no luck
string str
= assemblyName
.Split(new char[] { ',' })[0];
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
if (str.Equals("[CompanyName]"))
{
return Assembly.GetExecutingAssembly().GetType(typeName);
}
Sorry if you have seen this somewhere else, I'm just pretty desperate to get this to work for backwards compatability.