Using reflection and assembly.getTypes()

F

FBU

Hi,

I'm writing a C# application for my pocket pc in which i would like to load
dynamically dll (kind of plugins).
In the following piece of code, the load of the assembly (.dll) works
perfectly and my 'assembly' variable seems to be properly initialized. But,
once i call the function GetTypes(), an exception (TypeLoadException) is
thrown.
I don't find any good reasons why i get this exception...

Any help would be very much appreciated.

Thank you!

Frédéric de Buisseret.
public static object GetModuleFromDevice(ArrayList modules)
{
string path =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetN
ame().CodeBase);
path += @"\modules";

string[] files = Directory.GetFiles(path, "Module*.dll");
Assembly assembly;

foreach (string file in files) {
assembly = Assembly.LoadFrom(file);
if (assembly == null)
continue;

object module = null;
try{
// look in assembly types for our class
Type[] types = assembly.GetTypes(); // Exception
here

foreach (Type definedType in types) {
if (!definedType.IsClass)
continue;

if (definedType.Name.Substring(0, 7).CompareTo("Module_") ==
0) {
module = Activator.CreateInstance(definedType);
break;
}
}
}
catch (Exception e) {
...
}

.....
 
A

Alex Feinman [MVP]

I would suspect that the reason is a missing referenced assembly - something
that is referenced by your plugin assembly is not loaded yet. Try loading
such an assembly explicitly.

By the way, a better way to do this :
definedType.Name.Substring(0, 7).CompareTo("Module_")
is this:
definedType.Name.StartsWith("Module_")
String.StartsWith method is often overlooked.

I've put together a sample that loads plugins and invokes them. Pehaps it
would be of some use to you.
http://www.alexfeinman.com/download.asp?doc=PluginApp.zip
 
F

FBU

Alex,

I checked my project and find out the missing reference. It was even worse,
my test.dll was not compiled for the compact framework (shame on me!) and,
of course, the MScorlib was not referenced.

Again, many thanks for the sample and your help.

Regards,
Frédéric.


Alex Feinman said:
I would suspect that the reason is a missing referenced assembly - something
that is referenced by your plugin assembly is not loaded yet. Try loading
such an assembly explicitly.

By the way, a better way to do this :
definedType.Name.Substring(0, 7).CompareTo("Module_")
is this:
definedType.Name.StartsWith("Module_")
String.StartsWith method is often overlooked.

I've put together a sample that loads plugins and invokes them. Pehaps it
would be of some use to you.
http://www.alexfeinman.com/download.asp?doc=PluginApp.zip


--
Alex Feinman
---
Visit http://www.opennetcf.org
FBU said:
Hi,

I'm writing a C# application for my pocket pc in which i would like to load
dynamically dll (kind of plugins).
In the following piece of code, the load of the assembly (.dll) works
perfectly and my 'assembly' variable seems to be properly initialized. But,
once i call the function GetTypes(), an exception (TypeLoadException) is
thrown.
I don't find any good reasons why i get this exception...

Any help would be very much appreciated.

Thank you!

Frédéric de Buisseret.
public static object GetModuleFromDevice(ArrayList modules)
{
string path =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetN
ame().CodeBase);
path += @"\modules";

string[] files = Directory.GetFiles(path, "Module*.dll");
Assembly assembly;

foreach (string file in files) {
assembly = Assembly.LoadFrom(file);
if (assembly == null)
continue;

object module = null;
try{
// look in assembly types for our class
Type[] types = assembly.GetTypes(); // Exception
here

foreach (Type definedType in types) {
if (!definedType.IsClass)
continue;

if (definedType.Name.Substring(0,
7).CompareTo("Module_")
==
0) {
module = Activator.CreateInstance(definedType);
break;
}
}
}
catch (Exception e) {
...
}

.....
 

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