473,385 Members | 1,863 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,385 software developers and data experts.

"is" operator returning false when should be true?

Hello all;

Since I do have working code, this is more for my curiosity only.

I'm creating a "Plugin" architecture, following some of the many examples on
the 'net. Basically what I have is this:
- a DLL which contains the interface that every plugin must implement
(IPlugin). This DLL also contains a class with the ability to search for
DLLs in the calling applications working directory that contain classes
implementing IPlugin (I call this the PluginLoader). I fixed the version of
this DLL to 1.0.0.0, so the version doesn't change everytime it's built.
- a DLL which contains a class that implements IPlugin, called MyPlugin.
This obviously references the IPlugin DLL.
- a test EXE which tries to load a plugin. Again, this obviously references
the IPlugin dll.

All the references are to the Projects within VS.NET, not to the DLLs.

As you can assume, there was a function in the PluginLoader as follows:

private Type[] SearchDirectoryForPlugins(string path)

{

string[] dlls = Directory.GetFiles(path, "*.dll");

ArrayList pluginList = new ArrayList();

foreach(string dll in dlls)

{

try

{

Assembly a = Assembly.LoadFile(dll);

Type[] types = a.GetTypes();

foreach(Type t in types)

{

if (t is FullNamespace.IPlugin)

{

pluginList.Add(t);

}

}

}

catch(Exception e) //should handle specific exception

{

Console.WriteLine(e.Message);

//the dll wasn't a valid .NET assembly. Ignore.

}

}

return (Type[]) pluginList.ToArray(typeof(Type)); //confusing, but I
like arraylists!

}
My problem was that the "if (t is FullNamespace.IPlugin)" line was returning
false when it encountered a class that does indeed implement IPlugin, and I
had no idea why. I have since changed the code, removing the "is", and now
using the Type.FindInterfaces function - which works perfectly.

Would anyone have any idea why "is" would not work as expected in this
scenario, when FindInterfaces works fine??

Thanks,

Derrick
Nov 17 '05 #1
6 1955
> if (t is FullNamespace.IPlugin)

This line is asking if t, an object of type System.Type, implements
FullNamespace.IPlugin. You don't really care if System.Type implements
FullNamespace.IPlugin, what you care about is the type t represents.

--
Jonathan Allen
"Derrick" <de***********@hotmail.com> wrote in message
news:3D*******************@news20.bellglobal.com.. .
Hello all;

Since I do have working code, this is more for my curiosity only.

I'm creating a "Plugin" architecture, following some of the many examples
on the 'net. Basically what I have is this:
- a DLL which contains the interface that every plugin must implement
(IPlugin). This DLL also contains a class with the ability to search for
DLLs in the calling applications working directory that contain classes
implementing IPlugin (I call this the PluginLoader). I fixed the version
of this DLL to 1.0.0.0, so the version doesn't change everytime it's
built.
- a DLL which contains a class that implements IPlugin, called MyPlugin.
This obviously references the IPlugin DLL.
- a test EXE which tries to load a plugin. Again, this obviously
references the IPlugin dll.

All the references are to the Projects within VS.NET, not to the DLLs.

As you can assume, there was a function in the PluginLoader as follows:

private Type[] SearchDirectoryForPlugins(string path)

{

string[] dlls = Directory.GetFiles(path, "*.dll");

ArrayList pluginList = new ArrayList();

foreach(string dll in dlls)

{

try

{

Assembly a = Assembly.LoadFile(dll);

Type[] types = a.GetTypes();

foreach(Type t in types)

{

if (t is FullNamespace.IPlugin)

{

pluginList.Add(t);

}

}

}

catch(Exception e) //should handle specific exception

{

Console.WriteLine(e.Message);

//the dll wasn't a valid .NET assembly. Ignore.

}

}

return (Type[]) pluginList.ToArray(typeof(Type)); //confusing, but I
like arraylists!

}
My problem was that the "if (t is FullNamespace.IPlugin)" line was
returning false when it encountered a class that does indeed implement
IPlugin, and I had no idea why. I have since changed the code, removing
the "is", and now using the Type.FindInterfaces function - which works
perfectly.

Would anyone have any idea why "is" would not work as expected in this
scenario, when FindInterfaces works fine??

Thanks,

Derrick

Nov 17 '05 #2
Derrick,

You might want to check the fusion logs here to see what assemblies are
being loaded in your app. It sounds like there might be different versions
of the plugin assembly being loaded (for one reason or another), and as a
result, the call to is is not returning what you expect.

Also, you might want to try removing the reference and then re-adding it
in your plugin project, this might help.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Derrick" <de***********@hotmail.com> wrote in message
news:3D*******************@news20.bellglobal.com.. .
Hello all;

Since I do have working code, this is more for my curiosity only.

I'm creating a "Plugin" architecture, following some of the many examples
on the 'net. Basically what I have is this:
- a DLL which contains the interface that every plugin must implement
(IPlugin). This DLL also contains a class with the ability to search for
DLLs in the calling applications working directory that contain classes
implementing IPlugin (I call this the PluginLoader). I fixed the version
of this DLL to 1.0.0.0, so the version doesn't change everytime it's
built.
- a DLL which contains a class that implements IPlugin, called MyPlugin.
This obviously references the IPlugin DLL.
- a test EXE which tries to load a plugin. Again, this obviously
references the IPlugin dll.

All the references are to the Projects within VS.NET, not to the DLLs.

As you can assume, there was a function in the PluginLoader as follows:

private Type[] SearchDirectoryForPlugins(string path)

{

string[] dlls = Directory.GetFiles(path, "*.dll");

ArrayList pluginList = new ArrayList();

foreach(string dll in dlls)

{

try

{

Assembly a = Assembly.LoadFile(dll);

Type[] types = a.GetTypes();

foreach(Type t in types)

{

if (t is FullNamespace.IPlugin)

{

pluginList.Add(t);

}

}

}

catch(Exception e) //should handle specific exception

{

Console.WriteLine(e.Message);

//the dll wasn't a valid .NET assembly. Ignore.

}

}

return (Type[]) pluginList.ToArray(typeof(Type)); //confusing, but I
like arraylists!

}
My problem was that the "if (t is FullNamespace.IPlugin)" line was
returning false when it encountered a class that does indeed implement
IPlugin, and I had no idea why. I have since changed the code, removing
the "is", and now using the Type.FindInterfaces function - which works
perfectly.

Would anyone have any idea why "is" would not work as expected in this
scenario, when FindInterfaces works fine??

Thanks,

Derrick

Nov 17 '05 #3
Hi,

It's correct

t is instance of Type , as you can imagine Type do not implement IPlugin,
and is correct in returning false.

The thing that t has information about a class that does implement IPlugin
is not know.

That is why Type expose GetInterface method

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Derrick" <de***********@hotmail.com> wrote in message
news:3D*******************@news20.bellglobal.com.. .
Hello all;

Since I do have working code, this is more for my curiosity only.

I'm creating a "Plugin" architecture, following some of the many examples
on the 'net. Basically what I have is this:
- a DLL which contains the interface that every plugin must implement
(IPlugin). This DLL also contains a class with the ability to search for
DLLs in the calling applications working directory that contain classes
implementing IPlugin (I call this the PluginLoader). I fixed the version
of this DLL to 1.0.0.0, so the version doesn't change everytime it's
built.
- a DLL which contains a class that implements IPlugin, called MyPlugin.
This obviously references the IPlugin DLL.
- a test EXE which tries to load a plugin. Again, this obviously
references the IPlugin dll.

All the references are to the Projects within VS.NET, not to the DLLs.

As you can assume, there was a function in the PluginLoader as follows:

private Type[] SearchDirectoryForPlugins(string path)

{

string[] dlls = Directory.GetFiles(path, "*.dll");

ArrayList pluginList = new ArrayList();

foreach(string dll in dlls)

{

try

{

Assembly a = Assembly.LoadFile(dll);

Type[] types = a.GetTypes();

foreach(Type t in types)

{

if (t is FullNamespace.IPlugin)

{

pluginList.Add(t);

}

}

}

catch(Exception e) //should handle specific exception

{

Console.WriteLine(e.Message);

//the dll wasn't a valid .NET assembly. Ignore.

}

}

return (Type[]) pluginList.ToArray(typeof(Type)); //confusing, but I
like arraylists!

}
My problem was that the "if (t is FullNamespace.IPlugin)" line was
returning false when it encountered a class that does indeed implement
IPlugin, and I had no idea why. I have since changed the code, removing
the "is", and now using the Type.FindInterfaces function - which works
perfectly.

Would anyone have any idea why "is" would not work as expected in this
scenario, when FindInterfaces works fine??

Thanks,

Derrick

Nov 17 '05 #4
Thanks. What you and Jonathon have said does indeed make sense. I had
assumed that even though I had cast the object to a System.Type, the "is"
operator would still return True because the object still carries along all
the IPlugin "meta data" (for lack of a better term).

I guess I assumed wrong!

As I said in the OP, my code works just fine with the Type.FindInterfaces,
so I'll just leave it. I would have prefered to use "is" because it is much
cleaner.

Thank again,

Derrick
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uv**************@TK2MSFTNGP10.phx.gbl...
Hi,

It's correct

t is instance of Type , as you can imagine Type do not implement IPlugin,
and is correct in returning false.

The thing that t has information about a class that does implement IPlugin
is not know.

That is why Type expose GetInterface method

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Derrick" <de***********@hotmail.com> wrote in message
news:3D*******************@news20.bellglobal.com.. .
Hello all;

Since I do have working code, this is more for my curiosity only.

I'm creating a "Plugin" architecture, following some of the many examples
on the 'net. Basically what I have is this:
- a DLL which contains the interface that every plugin must implement
(IPlugin). This DLL also contains a class with the ability to search for
DLLs in the calling applications working directory that contain classes
implementing IPlugin (I call this the PluginLoader). I fixed the version
of this DLL to 1.0.0.0, so the version doesn't change everytime it's
built.
- a DLL which contains a class that implements IPlugin, called MyPlugin.
This obviously references the IPlugin DLL.
- a test EXE which tries to load a plugin. Again, this obviously
references the IPlugin dll.

All the references are to the Projects within VS.NET, not to the DLLs.

As you can assume, there was a function in the PluginLoader as follows:

private Type[] SearchDirectoryForPlugins(string path)

{

string[] dlls = Directory.GetFiles(path, "*.dll");

ArrayList pluginList = new ArrayList();

foreach(string dll in dlls)

{

try

{

Assembly a = Assembly.LoadFile(dll);

Type[] types = a.GetTypes();

foreach(Type t in types)

{

if (t is FullNamespace.IPlugin)

{

pluginList.Add(t);

}

}

}

catch(Exception e) //should handle specific exception

{

Console.WriteLine(e.Message);

//the dll wasn't a valid .NET assembly. Ignore.

}

}

return (Type[]) pluginList.ToArray(typeof(Type)); //confusing, but I
like arraylists!

}
My problem was that the "if (t is FullNamespace.IPlugin)" line was
returning false when it encountered a class that does indeed implement
IPlugin, and I had no idea why. I have since changed the code, removing
the "is", and now using the Type.FindInterfaces function - which works
perfectly.

Would anyone have any idea why "is" would not work as expected in this
scenario, when FindInterfaces works fine??

Thanks,

Derrick


Nov 17 '05 #5
> if (t is FullNamespace.IPlugin)
{
pluginList.Add(t);
}


The above block should be changed to:

<code>
if (typeof(FullNamespace.IPlugin).IsAssignableFrom(t) )
{
// add the plugin type
pluginList.Add(t);
}
</code>

Mark
Nov 17 '05 #6

Cool...thanks Mark. Never even thought of doing it that way!!

Derrick
"Mark Rockmann" <St**********@nospam.nospam> wrote in message
news:OA**************@TK2MSFTNGP10.phx.gbl...
if (t is FullNamespace.IPlugin)
{
pluginList.Add(t);
}


The above block should be changed to:

<code>
if (typeof(FullNamespace.IPlugin).IsAssignableFrom(t) )
{
// add the plugin type
pluginList.Add(t);
}
</code>

Mark

Nov 17 '05 #7

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

Similar topics

10
by: Sylvain Thenault | last post by:
Hi there ! Can someone explain me the following behaviour ? >>> l = >>> 0 in (l is False) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iterable argument...
24
by: hjbortol | last post by:
Hi! Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++? Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler dependent? Any references are welcome! Thanks in...
18
by: jrhoads23 | last post by:
Hello, I am trying to find a way to tell if an .NET windows forms Button (System.Windows.Forms.Button) is "depressed" (pushed down). For my application, I can not use a check box control set to...
3
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for...
9
by: Lonnie Princehouse | last post by:
There doesn't seem to be any way to customize the behavior of "is" as can be done for other operators... why not?
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
9
by: =?Utf-8?B?VG9ueQ==?= | last post by:
Can anybody tell why IsNumeric("5-") is true?
81
by: BlueJ774 | last post by:
Can someone please explain to me the difference between the "is" keyword and the == boolean operator. I can't figure it out on my own and I can't find any documentation on it. I can't...
3
by: Kosmos | last post by:
Hey ya'll...I can't seem to figure out why I'm getting this error message, but it all started when I added the new line of code with the recSet5.AddNew --- when I ran the first line, the logic worked...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.