PC Review


Reply
Thread Tools Rate Thread

InvalidCastExcetion after AppDomain.CreateInstanceFromAndUnwrap

 
 
dahwoud
Guest
Posts: n/a
 
      27th Apr 2004
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[i].Extension.Equals(".dll"))
{
///Loading DLL files dynamicaly
Assembly assembly = null;
Type[] types= null;
try
{
assembly = Assembly.LoadFrom(files[i].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[i].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 !!
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Y2hvcm5iZQ==?=
Guest
Posts: n/a
 
      29th Apr 2004
PS... I also don't bother using the AppDomain's CreateInstance... methods as anything you create from Assembly or Activator is implicitly created in the current AppDomain anyway. I reserve AppDomain instancing for when I truly need that level of insulation ( for shadow copying, for instance ).
 
Reply With Quote
 
 
 
 
New Member
Join Date: Apr 2012
Posts: 1
 
      17th Apr 2012
Having a very similar issue... wondering if you were able to resolve?>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resume after Hibernate or Standby fails. Power on after Shutdown is fine. Michael Shergold Windows XP Help 0 25th Mar 2005 05:17 PM
Nothing Clickable after second restart after system restore =?Utf-8?B?TWF0dEw=?= Windows XP Help 3 29th Nov 2004 01:15 AM
windows too big after after XP pro re-install Ken Wyatt Windows XP Help 1 14th Nov 2004 04:34 PM
programs stop working after a few hours after install =?Utf-8?B?SmVmZiBNYW5kcnlr?= Windows XP Help 0 25th Mar 2004 08:16 AM
XP home hangs after mup.sys after changing CD drive James Windows XP Performance 2 3rd Feb 2004 12:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:06 PM.