Is vs IsAssignableFrom to detect and interface

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi, I have an interface IMyInterface that is implemented by BaseClass. I
also have a class DerivedClass that is inherited from BaseClass.

Using reflection, I would like to be able to find DerivedClass by detecting
that it has implemented IMyInterface through BaseClass.

Here is my code to try and do so:

Call With:
------------
List<Type> ltTypes;
PluginLocator plLocator;
string sPath = @"c:\plugins\";

ltTypes = plLocator.FindTypesByInterface<IMyInterface>(sPath, "*.dll");
------------


FindTypesByInterface defined as:
------------
public List<Type> FindTypesByInterface<InterfaceType>(string path, string
searchpattern)
{
Assembly aAssembly;
Type[] tPossibleTypes;
List<Type> ltFoundTypes = new List<Type>();
string[] sAssemblies = Directory.GetFiles(path, searchpattern);

foreach (string sAssembly in sAssemblies)
{
aAssembly = Assembly.LoadFile(sAssembly);
tPossibleTypes = aAssembly.GetTypes();

foreach (Type type in tPossibleTypes)
{
//how come this DOES NOT work?
if (type is InterfaceType)
ltFoundTypes.Add(type);
else if (type.BaseType != null && type.BaseType is InterfaceType)
ltFoundTypes.Add(type);

//how come this DOES work?
//if (typeof(InterfaceType).IsAssignableFrom(type))
//
ltFoundTypes.Add(type);
}
}

return ltFoundTypes;
}
------------

As you can see, the is keyword does not detect the interface IMyInterface.
However, the method IsAssignableFrom does detect it. I was just curious why
this is and if I was using the is keyword incorrectly.

Thanks!


.



Relevant Pages