473,472 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Compare inteface Type with each other

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
Dec 22 '06 #1
4 2060
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
Dec 22 '06 #2
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" <Ni****@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
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
Dec 22 '06 #3
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" <Ni****@discussions.microsoft.comwrote in message
news:86**********************************@microsof t.com...
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
Dec 23 '06 #4
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
Dec 23 '06 #5

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

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
8
by: Marius Horak | last post by:
I've never used Interface and never will. But now I have to make major modifications to a large system where almost all classes are based on some kind of Interface. Hundreds of classes. When I...
5
by: Edd E | last post by:
Hi, I have a database to store my analyses (Access 2002, WinXp), the basic structure is: TABLE 1 = Sample Info TABLE 2 = Analysis type 1 TABLE 3 = Analysis type 2 TABLE 4 = Analysis type 3 ...
5
by: S S | last post by:
Hi I have a requirement where I am declaring a map within a class. class abc { map <void*, void*mMap; // I do not pass compare struct here. .... }; Here I am not passing compare function,...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.