Serialization and de-serialization of types loaded from assembly at runtime

M

mookid8000

Hi group!

Is it possible to scan an assembly *which has NOT been added as a
reference* for types, and dynamically create instances of the loaded
types?

I have a program, MyProgram.exe, which at runtime uses
Asseembly.LoadFile(...) to load an assembly, MyAssembly.dll. When
MyAssembly.dll is added as a reference to the MyProgram project,
serialization and de-serialization works lige a charm.

However, when I remove the reference to MyAssembly.dll in the MyProgram
project, I get an exception upon de-serialization.

Is there something I can do which is like "adding an assembly as a
reference at runtime"?

The best,
Mogens
 
G

Guest

Howdy,

Yes it's possible and quite simple. Have a look at my short C# example

private void button3_Click(object sender, System.EventArgs e)
{

System.Reflection.Assembly assembly
=
System.Reflection.Assembly.LoadFile("C:\\WINDOWS\\assembly\\GAC\\Microsoft.VisualBasic\\7.0.5000.0__b03f5f7f11d50a3a\\Microsoft.VisualBasic.dll");

listBox1.BeginUpdate();
foreach (System.Type type in assembly.GetExportedTypes())
{
listBox1.Items.Add(type.FullName);
if (type.IsClass && !type.IsAbstract)
{
object instance = System.Activator.CreateInstance(type);
}
}
listBox1.EndUpdate();

}
 
N

Nicolas Guinet

Is it possible to scan an assembly *which has NOT been added as a
reference* for types, and dynamically create instances of the loaded
types?

YES IT IS

something like this (untested) :

Assembly myAss = Assembly.Load("myDll.dll");

Type t = myAss.GetType("namespacemyDll.typeName");
ConstructorInfo ci = new t.GetConstructor( new t[0] );
object o = ConstructorInfo.Invoke( new object[0] );
// now o is a ref. to an anstance of namespacemyDll.typeName

or

foreach( Type t in myAss.GetTypes() )
{
Console.WriteLine( "object: " + t.ToString() );
object o = Activator.CreateInstance(type);
// and so on ....foreach( MethodInfo m in t.GetMethods() ....
}


Nicolas Guinet
www.nicolasguinet.com
 
M

mookid8000

Thanks for the replies guys, but I think neither of you really
understood my problem.

I do use Assembly.LoadFile to load an assembly, and I do use
GetExportedTypes to iterate through all types and look for subclasses
of one particular class.

The problem arises when the user of my program saves his work. At this
point all instances of the dynamically loaded types will be serialized
into a file.

Now, when the user quits the program and tries to load his work, a
SerializationException occurs.

I can solve my problem temporarily by adding the assemblies I want to
load at runtime as references to the project, but this does not solve
my problem entirely, because I want to be able to load the assemblies
truly at runtime.
 
Y

Yury

You should make XmlSerializer aware of all types in your objects net.
Use following constructor to specify extra types.

System.Xml.Serialization.XmlSerializer(Type type, Type [] extraTypes)
 
D

David Levine

You can subscribe to the AssemblyResolve event; the deserializer will
generate an event for each assembly that needs to be loaded.
 
M

mookid8000

I did this, and it seems to work most of the time. However, it seems
sometimes the event handler does not get called when it should. But I
will figure out why that is on my own...

Thanks for the quick reply!

-Mogens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top