all groups > dotnet compact framework > august 2004 >
You're in the

dotnet compact framework

group:

Using reflection and assembly.getTypes()



Re: Using reflection and assembly.getTypes() Alex Feinman [MVP]
8/7/2004 2:00:29 PM
dotnet compact framework: 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
[quoted text, click to view]

Using reflection and assembly.getTypes() FBU
8/7/2004 8:49:43 PM
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) {
...
}

.....

Re: Using reflection and assembly.getTypes() FBU
8/8/2004 12:37:24 AM
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.


[quoted text, click to view]

AddThis Social Bookmark Button