Comparing same interface type returns false, Why?

J

Joe Black

Hi all,

Using Windows CP Pro
VS .net 2005

I'm creating an app that allows user to extend its functionality by
installing plug in modules, these modules support an interface I have
created called IPlugInInterface. The problem is when I come to load them
I check each dll to see if supports this interface, they all return
false even though when I inspect them during debugging they look the
same, I use the following function:

public ArrayList SearchPath(string dir, AppDomain domain)
{
ArrayList ar = new ArrayList();

foreach (string File in System.IO.Directory.GetFiles(dir, "*.dll"))
{
try
{
Type ct = typeof(TestNamespace.PlugIn.IPlugInInterface);
System.Reflection.Assembly asm =
System.Reflection.Assembly.LoadFrom(File);

foreach (Type t in asm.GetTypes())
{
foreach (Type iface in t.GetInterfaces())
{
//*******************************
// see text below
//*******************************
if (ct.IsAssignableFrom(iface))
{
ar.Add(t);
break;
}
}
}
}
catch (Exception ex)
{
}
}
return ar;
}


//******************

this is where the problem occurs, if I inspect both ct and iface both
seem to be of the same type, ie namespace and name etc but the call to
IsAssignableFrom always returns false.

Any ideas

TIA

Joe
 
M

Ming Chen

Hi, Joe.

The plug in classes would have to implement the exact same interface, not
only the namespace qualified interface name, but also the same assembly and
version. Check AssemblyQualifiedName property of both types, they have to be
the same for IsAssignableFrom to return true.
 
J

Joe Black

Ming said:
Hi, Joe.

The plug in classes would have to implement the exact same interface, not
only the namespace qualified interface name, but also the same assembly and
version. Check AssemblyQualifiedName property of both types, they have to be
the same for IsAssignableFrom to return true.

Hi Ming,

I have the interface declared within an external DLL file as follows

public interface IPlugInInterface
{
#region Variable declaration

int PlugInType { get;}
string PlugInName { get;}
string PlugInDescription { get;}
string PlugInShortDescription { get;}
int PlugInMajorVersion { get;}
int PlugInMinorVersion { get;}
char PlugInRevision { get;}
string PlugInGuid { get;}
string PlugInReleaseDate { get;}
string PlugInAuthor { get;}
string PlugInAuthorInformation { get;}
bool RequiresRegistration { get;}

#endregion

#region Member function declaration

void PerformAction(IPlugInContext context);
void ShowForm(System.Windows.Forms.Control parent);

#endregion
}

I then declare my class:

public class MyPlugin:IPlugInInterface
{
//code goes here
}

Does this not mean I am using the exact same interface, or am I missing
something?

Your help is appreciated.

Joe
 
M

Ming Chen

You have to declare the interface in a single assembly for all PlugIns and
having all other assemblies reference to this one. .NET class/interface have
their own internal type identifier hence they are not simply matched base on
memory layout. Types declared in different dlls are different types even if
their definitions are exactly the same.

For example, you can create a dll (PluginInterface.dll) which contains
nothing but the definition of the interface, then create individual PlugIn
classes by adding reference to this dll.
 
J

Joe Black

Ming said:
You have to declare the interface in a single assembly for all PlugIns and
having all other assemblies reference to this one. .NET class/interface have
their own internal type identifier hence they are not simply matched base on
memory layout. Types declared in different dlls are different types even if
their definitions are exactly the same.

For example, you can create a dll (PluginInterface.dll) which contains
nothing but the definition of the interface, then create individual PlugIn
classes by adding reference to this dll.

Thanks Ming,

I found that I had a copy of the dll that implemented the
IPlugInInterface in the same directory that the dll with the actual plug
in was in, this seemed to cause the appdomain to look at that copy of
the IPlugInInterface, hence the failure of the function call.

Many thanks for your help

Joe
 
G

Guest

hi, plz try the following (it always works 4 me)

Type _type = somePlugIn.GetType();

foreach (Type iface in _type.GetInterfaces()) {
if (iface == typeof(IPlugInInterface)) {
//do some magic
}
}

regards
 

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