PC Review


Reply
Thread Tools Rate Thread

Compare inteface Type with each other

 
 
=?Utf-8?B?TmlrbGFz?=
Guest
Posts: n/a
 
      22nd Dec 2006
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2lhcmFuIE8nJycnRG9ubmVsbA==?=
Guest
Posts: n/a
 
      22nd Dec 2006
You could use :
The t.TypeHandle.Value property from each type to compare.


--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com


"Niklas" wrote:

> 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

 
Reply With Quote
 
aejw.com
Guest
Posts: n/a
 
      22nd Dec 2006
Because when you refer to "interfaceType", you are refering to the object,
not the objects type... try using "interfaceType.GetType()"

Adam - http://www.aejw.com/?page=contact


"Niklas" <(E-Mail Removed)> wrote in message
news:86D08BD6-6FF5-4E5C-8860-(E-Mail Removed)...
> 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


 
Reply With Quote
 
=?Utf-8?B?TWFyayBSLiBEYXdzb24=?=
Guest
Posts: n/a
 
      23rd Dec 2006
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
--
http://www.markdawson.org
http://themightycoder.spaces.live.com


"aejw.com" wrote:

> Because when you refer to "interfaceType", you are refering to the object,
> not the objects type... try using "interfaceType.GetType()"
>
> Adam - http://www.aejw.com/?page=contact
>
>
> "Niklas" <(E-Mail Removed)> wrote in message
> news:86D08BD6-6FF5-4E5C-8860-(E-Mail Removed)...
> > 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

>

 
Reply With Quote
 
=?Utf-8?B?TWFyayBSLiBEYXdzb24=?=
Guest
Posts: n/a
 
      23rd Dec 2006
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.
--
http://www.markdawson.org
http://themightycoder.spaces.live.com

"Niklas" wrote:

> Hi
> I found the problem...nUnit. In nUnit (interfaceType ==
> typeof(IInstrumentAdministrator) this is false, but if I run the same code in
> a Console application it is true.
> Regards
> /Niklas
>
> "Niklas" wrote:
>
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GUI Inteface for SC Reed57 Windows Vista General Discussion 2 23rd Mar 2007 08:15 PM
Inteface for Controls.Add(???) Matthias Langbein Microsoft C# .NET 1 23rd Sep 2006 12:23 AM
Compare MethodInfo to delegate type? lee.chappers@googlemail.com Microsoft Dot NET Framework 1 15th Mar 2006 04:35 PM
Problem in compare text-type with a number =?Utf-8?B?QW5kcmVhc00=?= Microsoft Access Queries 4 2nd Mar 2005 12:53 AM
about the inteface layout authorking Microsoft C# .NET 0 19th Apr 2004 03:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:36 PM.