473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Comparing same interface type returns false, Why?

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
Nov 17 '05 #1
5 3153
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.

--
Ming Chen
http://engdump.blogspot.com/
"Joe Black" <Jo******@newsgroup.nospam> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
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

Nov 17 '05 #2
Ming Chen wrote:
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
Nov 17 '05 #3
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.

--
Ming Chen
http://engdump.blogspot.com/
"Joe Black" <Jo******@newsgroup.nospam> wrote in message
news:OA*************@TK2MSFTNGP15.phx.gbl...
Ming Chen wrote:
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

Nov 17 '05 #4
Ming Chen wrote:
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
Nov 17 '05 #5
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
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kill Bill | last post by:
type(i) == "<type 'float'>" this always returns false. How come? type(i)returns <type 'float'> if i is a float so why isn't == working?
0
by: Farek | last post by:
(If Im posting in the wrong place concerning COM+ and .NET plz redirect me.) Hello all, Im writing a COM+ in VB.NET that is suppose to be able to set/get an address(String value). I've made...
3
by: Simon Harvey | last post by:
Hi all, I hope someone can help me with the following: I have a number of usercontrols that I've made which provides certain audit functions for any data inserted into it. Each audit control...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
0
by: Ken | last post by:
Hi I have a little application that does datavalidation. It supports dynamically loaded plugins (you drop a dll with a class implementing IValidator<Tin the same dir as the main application)....
8
by: Lamefif | last post by:
// C3DRect supports IDraw and IShapeEdit. class C3DRect : public IDraw, public IShapeEdit { public: C3DRect(); virtual ~C3DRect(); // IDraw virtual void Draw(); // IShapeEdit virtual void...
4
by: jmDesktop | last post by:
In the code below from MSDN How do the PeopleEnum methods ever get called? foreach (Person p in peopleList) Console.WriteLine(p.firstName + " " + p.lastName); What is going on behind the...
0
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
D'Arcy J.M. Cain wrote: I can give you the technical answer after reading the sources of the decimal module: you can only compare to Decimal what can be converted to Decimal. And that is int,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.