473,480 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

GetInterface() without a string


If I have an object o and want to check if it's a certain interface,
I write like this:

bool oIsIMyInterface = o is IMyInterface;

However, if I have a Type object t, and I want to check if it's a
certain interface, I have to write the interface name as a string:

bool tIsIMyInterface = t.GetInterface("MyNamespace.IMyInterface") != null;

Is there any way to do this without putting the interface between
quotes, i.e. without writing it as a string, i.e. getting it
type-checked etc by the compiler?

Something along these lines:

bool tIsIMyInterface = t.IsSubclassOf(typeof(IMyInterface)) != null;
Regards,
/Gomaw

Nov 15 '05 #1
3 5835
Hey,

you can do this with the object itself. When you have an object o which
implements an interface i,
"o is i" will return true. So you can write: if(o is i) { // o implements
the interface i}
For further info lookup the is ( and as) operators.

Greetz,
-- Rob.

Gomaw Beoyr wrote:
If I have an object o and want to check if it's a certain interface,
I write like this:

bool oIsIMyInterface = o is IMyInterface;

However, if I have a Type object t, and I want to check if it's a
certain interface, I have to write the interface name as a string:

bool tIsIMyInterface = t.GetInterface("MyNamespace.IMyInterface")
!= null;

Is there any way to do this without putting the interface between
quotes, i.e. without writing it as a string, i.e. getting it
type-checked etc by the compiler?

Something along these lines:

bool tIsIMyInterface = t.IsSubclassOf(typeof(IMyInterface)) !=
null;
Regards,
/Gomaw

Nov 15 '05 #2
"Gomaw Beoyr" <Go*********@no.spam.please.no> wrote in message
news:OH*************@TK2MSFTNGP12.phx.gbl...

If I have an object o and want to check if it's a certain interface,
I write like this:

bool oIsIMyInterface = o is IMyInterface;

However, if I have a Type object t, and I want to check if it's a
certain interface, I have to write the interface name as a string:

bool tIsIMyInterface = t.GetInterface("MyNamespace.IMyInterface") != null;
Is there any way to do this without putting the interface between
quotes, i.e. without writing it as a string, i.e. getting it
type-checked etc by the compiler?

Something along these lines:

bool tIsIMyInterface = t.IsSubclassOf(typeof(IMyInterface)) != null;


Check out System.Type.IsAssignableFrom().
Nov 15 '05 #3
In article <eF**************@TK2MSFTNGP10.phx.gbl>,
Ro********@student.tul.edu (Rob Tillie) writes:
you can do this with the object itself. When you have an object o which
implements an interface i,
"o is i" will return true. So you can write: if(o is i) { // o implements
the interface i}


Yes, thanks, but my problem was that I don't have an object. The
program checks if the class (represented by a Type object) implements
the interface, and if it does, an object is instantiated with
Activator.CreateInstance().

The way to solve the problem was posted by Mike Schilling, where t
is the Type object:

bool tIsIMyInterface = typeof(IMyInterface).IsAssignableFrom(t);

(I assume that the only way an interface can be assignable from a
type is if the type implements the interface...)

Gomaw

Nov 15 '05 #4

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

Similar topics

14
2879
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
5
6126
by: reddy | last post by:
I am trying to insert a node into an XMLFile. using XMLTextwriter. My Question is Is it possible to do without using XMLDocument. Because its loading all the the file into memory. I just want to...
14
11015
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
3
23925
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
1
1377
by: Peter Nofelt | last post by:
Hey all, I want to return an xml structure without .net trying to inject any of its xml schema? Can this be done? Here is the scenario: I'm running into an issue with the return string of my...
1
7610
by: Scanner2001 | last post by:
I am looking for a way to return the output of a web service as a string without the xml tags, just the raw data. I am calling the web service from an html page using a form post method....
5
7804
by: mmcd79 | last post by:
I built a VB.net application that makes use of a machine level DB connection string setting, and a user level starting location setting. The machine level setting and the default user based...
14
3141
by: Daniel Lidström | last post by:
Hello! I have just discovered a way to use the private implementation idiom (pimpl), without the overhead of dynamic memory allocation. For those of you who don't know what this is, Wikipedia...
3
5216
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
7055
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
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
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
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4501
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
3013
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
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
572
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.