471,624 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

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

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

Feb 24 '06 #1
6 2155
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();

}
--
Milosz Skalecki
MCP, MCAD
"mo********@gmail.com" wrote:
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

Feb 24 '06 #2
> 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

<mo********@gmail.com> a écrit dans le message de news:
11*********************@u72g2000cwu.googlegroups.c om... 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

Feb 24 '06 #3
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.

Feb 28 '06 #4
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)

Feb 28 '06 #5
You can subscribe to the AssemblyResolve event; the deserializer will
generate an event for each assembly that needs to be loaded.

<mo********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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.

Feb 28 '06 #6
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

Mar 1 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by WebRod | last post: by
reply views Thread by Laurent Pointal | last post: by
reply views Thread by Michael Dyson | last post: by
reply views Thread by Alejandro Scomparin | last post: by
1 post views Thread by gandalf | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.