D
dahwoud
This exception is driving me crazy for now ... i've read a lot of
online documents, articles, microsoft.public.dotnet.* archives and i
can't correct this error.
The idea is simple (with Dotnet): i'm writing an application with
plugin support.
the plugin support is added with the help of C# interfaces.
here comes the plugin interface that is part of my main application
namespace
using System;
namespace MyApp.plugin
{
public interface IMyAppPlugin
{
void Load();
void Unload();
void DoProcess(object o);
bool Active {get;set;}
...
}
}
Ok ... until now, nothing fancy you could say ...
Then here comes a simple plugin available as managed class library
(aka DLL):
namespace IPBlocklist
{
public class IPBlocklist: IMyAppPlugin
{
public void Load() {...}
public void Unload() {...}
public void DoProcess(object o) {... //somme processing}
...
...
}
}
This tiny class is compiled in the dll "IPBlocklist.dll"
Now, here comes the code generating the error, from the main
application, i use dynamic class loading by searching in \plugin
directory dll that exposes class implementing interface IMyAppPlugin.
When i'm ok, the class is loaded ...
///Loading MyDC plugin .NET DLL
///Getting the plugin Directory
DirectoryInfo dir = new
DirectoryInfo(Directory.GetCurrentDirectory()+"\\plugin");
///Getting files in directory
FileInfo[] files = dir.GetFiles();
for(int i=0;i<files.Length;i++)
{
///Looking at only DDL files
if(files.Extension.Equals(".dll"))
{
///Loading DLL files dynamicaly
Assembly assembly = null;
Type[] types= null;
try
{
assembly = Assembly.LoadFrom(files.FullName);
types = assembly.GetTypes();
}
catch(Exception ee)
{
OnLog(ee,LoggerLevel.Error);
//If somethins's wrong stop here for this dll and look at next one
continue;
}
for(int j=0;j<types.Length;j++)
{
try
{
if(types[j].GetInterface("MyApp.IMyAppPlugin",true) != null)
{
object o = assembly.CreateInstance("MyApp.IMyAppPlugin",
false,
BindingFlags.CreateInstance,
null,
null,
null,
null);
object oi = AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap(files.FullName,types[j].FullName);
///
///The excetion is raised when i try to call one of IMyAppPlugin
interface methods. The exception is of type InvalidCastException
///
((MyApp.IMyAppPlugin)oi).Load();
break;
}
}
catch(Exception ee)
{
OnLog("Error while loading "+types[j].FullName,LoggerLevel.Error);
OnLog(ee,LoggerLevel.Error);
break;
}
}
}
}
What's wrong ?
Thanks for any help you could give me !!
online documents, articles, microsoft.public.dotnet.* archives and i
can't correct this error.
The idea is simple (with Dotnet): i'm writing an application with
plugin support.
the plugin support is added with the help of C# interfaces.
here comes the plugin interface that is part of my main application
namespace
using System;
namespace MyApp.plugin
{
public interface IMyAppPlugin
{
void Load();
void Unload();
void DoProcess(object o);
bool Active {get;set;}
...
}
}
Ok ... until now, nothing fancy you could say ...
Then here comes a simple plugin available as managed class library
(aka DLL):
namespace IPBlocklist
{
public class IPBlocklist: IMyAppPlugin
{
public void Load() {...}
public void Unload() {...}
public void DoProcess(object o) {... //somme processing}
...
...
}
}
This tiny class is compiled in the dll "IPBlocklist.dll"
Now, here comes the code generating the error, from the main
application, i use dynamic class loading by searching in \plugin
directory dll that exposes class implementing interface IMyAppPlugin.
When i'm ok, the class is loaded ...
///Loading MyDC plugin .NET DLL
///Getting the plugin Directory
DirectoryInfo dir = new
DirectoryInfo(Directory.GetCurrentDirectory()+"\\plugin");
///Getting files in directory
FileInfo[] files = dir.GetFiles();
for(int i=0;i<files.Length;i++)
{
///Looking at only DDL files
if(files.Extension.Equals(".dll"))
{
///Loading DLL files dynamicaly
Assembly assembly = null;
Type[] types= null;
try
{
assembly = Assembly.LoadFrom(files.FullName);
types = assembly.GetTypes();
}
catch(Exception ee)
{
OnLog(ee,LoggerLevel.Error);
//If somethins's wrong stop here for this dll and look at next one
continue;
}
for(int j=0;j<types.Length;j++)
{
try
{
if(types[j].GetInterface("MyApp.IMyAppPlugin",true) != null)
{
object o = assembly.CreateInstance("MyApp.IMyAppPlugin",
false,
BindingFlags.CreateInstance,
null,
null,
null,
null);
object oi = AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap(files.FullName,types[j].FullName);
///
///The excetion is raised when i try to call one of IMyAppPlugin
interface methods. The exception is of type InvalidCastException
///
((MyApp.IMyAppPlugin)oi).Load();
break;
}
}
catch(Exception ee)
{
OnLog("Error while loading "+types[j].FullName,LoggerLevel.Error);
OnLog(ee,LoggerLevel.Error);
break;
}
}
}
}
What's wrong ?
Thanks for any help you could give me !!