473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Interface at run time

Is there a way I can find out at run time whether a particular interface has
been implemented by an instance of a class.

Thanks for any help
Fred
Nov 21 '05 #1
8 1093

"fred" <fr**@NoSpam.com> wrote
Is there a way I can find out at run time whether a particular interface has
been implemented by an instance of a class.

Try TypeOf:

If TypeOf SomeObject Is IDisposable Then SomeObject.Dispose
LFS
Nov 21 '05 #2

"fred" <fr**@NoSpam.com> wrote
Is there a way I can find out at run time whether a particular interface has
been implemented by an instance of a class.

Try TypeOf:

If TypeOf SomeObject Is IDisposable Then SomeObject.Dispose
LFS
Nov 21 '05 #3
You can use the is keyword such as

dim i as IMyInterface=nothing
if(myobject is IMyInterface) then
i=DirectCast(myobject, IMyInterface)
'use the interface here...
i.MyMethod()
end if
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"fred" <fr**@NoSpam.com> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
Is there a way I can find out at run time whether a particular interface has been implemented by an instance of a class.

Thanks for any help
Fred

Nov 21 '05 #4
You can use the is keyword such as

dim i as IMyInterface=nothing
if(myobject is IMyInterface) then
i=DirectCast(myobject, IMyInterface)
'use the interface here...
i.MyMethod()
end if
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"fred" <fr**@NoSpam.com> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
Is there a way I can find out at run time whether a particular interface has been implemented by an instance of a class.

Thanks for any help
Fred

Nov 21 '05 #5
Bob,

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> schrieb:
You can use the is keyword such as

dim i as IMyInterface=nothing
if(myobject is IMyInterface) then


That would work in C#. For VB.NET, use 'If TypeOf ... Is ... Then...'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #6
Bob,

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> schrieb:
You can use the is keyword such as

dim i as IMyInterface=nothing
if(myobject is IMyInterface) then


That would work in C#. For VB.NET, use 'If TypeOf ... Is ... Then...'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #7
In addition to other replies, you can also use IsAssignableFrom method of
the Type Class. Here's an example:

Public Class TestClass
Implements IDisposable

Public Sub Dispose() _
Implements System.IDisposable.Dispose

End Sub
End Class

MessageBox.Show( _
GetType(IDisposable).IsAssignableFrom( _
GetType(TestClass)).ToString)

This will return True since TestClass does implement the IDisposable
interface.

Just an additional option...
hope that helps..
Imran.
"fred" <fr**@NoSpam.com> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
Is there a way I can find out at run time whether a particular interface
has
been implemented by an instance of a class.

Thanks for any help
Fred

Nov 21 '05 #8
oops... typeof..

Thanks Herfried... :-)

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Bob,

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> schrieb:
You can use the is keyword such as

dim i as IMyInterface=nothing
if(myobject is IMyInterface) then


That would work in C#. For VB.NET, use 'If TypeOf ... Is ... Then...'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #9

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

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
4
by: yoz | last post by:
Hi everyone, I wonder if you can shed some light on a problem I have. I am exporting a C# .Net set of classes to COM. All of it is exported properly but the interface inheritences are not in the...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
9
by: msbs1984 | last post by:
Difference Between Interface and Abstract Class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
0
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
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...
0
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,...

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.