HOW TO : Serialization and "external" Assembly

  • Thread starter Thread starter Paul Bawin
  • Start date Start date
P

Paul Bawin

Hi All,

How can I load an object A that host an object B that is defined in an other
Assembly ?

Thank you

See the exemple below :

I have a DLL with these classes

namespace RTECH
{
[Serializable()]
public class Main // A Class that host a wizard
{
public Main()
{
}
public Wizard Wizard
{
get { return _Wizard; }
set { _Wizard = value; }
}
}

[Serializable()]
public abstract class Wizard // A Abstract Class Wizard
{
protected static string _Name;

}
}

I have an other DLL with this class

using RTECH;
namespace RTECH.Demo
{
[Serializable()]
public class WizardDemo : Wizard
{
public WizardDemo() : base()
{
}
}
}


Now I have an EXE with this code
Using RTECH;
.....
{
Main _main = new Main();
//I load the plugin that contain the WizardDemo
Assembly _assembly =
Assembly.LoadFile(@"C:\Sources\TestWizard\WizardDemo\bin\Debug\WizardDemo.dl
l");
Wizard _wizard =
(Wizard)_assembly.CreateInstance("RTECH.Demo.WizardDemo");
_main.Wizard = _wizard;

FileStream f = new FileStream(@"c:\sources\testwizard\test.bin",
System.IO.FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(f, _main);
f.Close();

f = new FileStream(@"c:\sources\testwizard\test.bin",
System.IO.FileMode.Open);
bf = new BinaryFormatter();
Main _main2 = (Main)bf.Deserialize(f); //Problem here
f.Close();
}
 
Paul,

The way to get around this is to make sure that you have BOTH assemblies
loaded before you attempt to deserialize the stream.

Hope this helps.
 
Back
Top