473,545 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ 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.IM yInterface") != 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(IMyInter face)) != null;
Regards,
/Gomaw

Nov 15 '05 #1
3 5837
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.IM yInterface")
!= 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(IMyInter face)) !=
null;
Regards,
/Gomaw

Nov 15 '05 #2
"Gomaw Beoyr" <Go*********@no .spam.please.no > wrote in message
news:OH******** *****@TK2MSFTNG P12.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.IM yInterface") != 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(IMyInter face)) != null;


Check out System.Type.IsA ssignableFrom() .
Nov 15 '05 #3
In article <eF************ **@TK2MSFTNGP10 .phx.gbl>,
Ro********@stud ent.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.Creat eInstance().

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

bool tIsIMyInterface = typeof(IMyInter face).IsAssigna bleFrom(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
2889
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: $Revision: 0.0 $
5
6139
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 insert in the front. My code is give below. Is it possible to do without using XMLDOcument? Dim masterDoc As String =...
14
11033
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 window.open function? I would prefer not to create a separate HTML page. So far all I have is the basic var cwin =...
3
23937
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
1381
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 .NET webservice. I am attempting to return an xml string similar to this: <?xml version="1.0" encoding="utf-8" ?>
1
7617
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. Specifically, I want to remove the <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://www.company.com/webservices/">string</string> And just...
5
7824
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 setting is of course stored in the app.exe.config file located in the same directory as the exe. Upon closing the form, I save the user setting which...
14
3149
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 has a nice article you can read. Anyway, I discovered that if you make all members in the implementation class mutable, you can in fact use this idiom...
3
5224
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 DataTable, I am using List of Objects. Here's one example: http://ryanolshan.com/technology/gridview-without-datasourcecontrol-datasource/
0
7499
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3490
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.