Assembly.CreateInstance returns a type that generates InvalidCastException when typecasted 
Author Message
 Assembly.CreateInstance returns a type that generates InvalidCastException when typecasted

I have a few projects in my solution. The interface Plugin is in the
Controls project, and then I have a XXXForm project that has a class that
implements the interface Plugin .
Then I have a Toolset project that loops through the directory for dll and
locates dll that implements the interface Plugin and use them.

If I only load a single assembly, the code is working. However, when I load
a few assemblies, the code is not working. The code snipper is below. The
logic and construct is correct, since I done a few 'debugging' myself by
blocking out codes, removing dlls until only one is left, etc. But it
doesn't work when I have made more than one call of LoadFrom for more than
one assembly file. If it's just one, it's fine. Anyone has any clue?

string[] fileEntries =
System.IO.Directory.GetFiles("E:\\development\\projects\\mmorpg\\plugins\\to
ols", "*.dll");

foreach(string fileName in fileEntries)

{

System.Reflection.Assembly objAssembly = null;

objAssembly = System.Reflection.Assembly.LoadFrom(fileName);

foreach(Type type in objAssembly.GetTypes())

{

if(null != type.GetInterface(typeof(Toolset.Plugin).FullName))

{

Plugin obj = (Plugin)System.Activator.CreateInstance(type);

menuItem_Tools.MenuItems.Add(obj.GetMenuItem());

Quote:
}
}
}

Cheers


Wed, 29 Jun 2005 13:32:15 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. generating COM type library from .NET assembly

2. Reflection - GetType, returning type not in assembly??!

3. Assembly.CreateInstance

4. Assembly.CreateInstance Problem

5. Using types in a different assembly given that the type may be used or not used

6. Help with: C2392 - covariant returns types are not supported in managed types

7. undefined type in arg list vs. return type

8. HRESULT return type turns into void when adding class from type library

9. Newbie: generate assembly code

10. Smart Pointer CreateInstance returns "0x80020008 Bad variable type error"

11. Assembly.Location returning shadowed location

12. returning a string array of my resources from an assembly

 

 
Powered by phpBB® Forum Software