Compare inteface Type with each other

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

Look at the TODO in the code. The commented code do not work, but the code
below the commented one does. How do I do to compare Types?

foreach (Type typeInAssembly in typesInAssemply)
{
// typeLoadException.Types can generate null values.
if (typeInAssembly != null && !typeInAssembly.IsAbstract)
{
foreach (Type interfaceType in typeInAssembly.GetInterfaces())
{
//TODO: Whats wrong with this: if (interfaceType ==
typeof(IInstrumentAdministrator) ???
if (interfaceType.Name == typeof(IInstrumentAdministrator).Name)
{
typesWithInterface.Add(typeInAssembly);
}
}
}
}

Regards
/Niklas
 
If you do interfaceType.GetType() that is going to return a
System.RuntimeType, since it is the type of the Type in question :-) which is
not going to help the issue. I believe the problem the OP may be having is
that the fully qualified names of the type do not match because they have
different values for the module.

Mark
 
I don't think nUnit has any problem, but someting with your codes setup. Is
the IInstrumentationAdministrator interface defined in a single file that is
compiled separately into the assembly you loaded and referenced by the
executing code. If this is the case then when you are iterating through the
types from the loaded assembly you will get to a
IInstrumentationAdministrator type but its fully qualified name will include
the module it came from i.e. myAssembly1.dll, whereas you executing code is
in a different module i.e. myExecutable.exe ad if that one included the
interface definition .cs file then when you say
typeof(IInstrumentationAdministrator) then the module of that type is going
to be myExecutable.exe, hence when you compare the two types they are
considered to be different because their modules are different. The code
would work for the .Name comparison since that compares just the simple name
of the type which is not taking into account the module name.

This is just my guess, if you break in the debugger on the line where you
compare the types and it is failing, look at the module name and see if they
are different.

Mark.
 

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

Back
Top