473,387 Members | 1,379 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,387 software developers and data experts.

How to tell if an object inherits an interface

Ron
I have a situation where I need to test if a Control implements from a
specific Interface to avoid an invalid cast exception:

foreach (Control ctrl in this.Controls)
{
// Need to test to see if ctrl inherits interface ICustomControl here
ICustomControl control = (ICustomControl)ctrl;
}

Thanks!
Ron

Mar 11 '08 #1
3 1819
if ( control is ICustomControl )
{
.....
}
Ron wrote:
I have a situation where I need to test if a Control implements from a
specific Interface to avoid an invalid cast exception:

foreach (Control ctrl in this.Controls)
{
// Need to test to see if ctrl inherits interface ICustomControl here
ICustomControl control = (ICustomControl)ctrl;
}

Thanks!
Ron
Mar 11 '08 #2
Ron
Thanks Darren!

"Darren" <Da****@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
if ( control is ICustomControl )
{
....
}
Ron wrote:
>I have a situation where I need to test if a Control implements from a
specific Interface to avoid an invalid cast exception:

foreach (Control ctrl in this.Controls)
{
// Need to test to see if ctrl inherits interface ICustomControl here
ICustomControl control = (ICustomControl)ctrl;
}

Thanks!
Ron
Mar 11 '08 #3
On Mar 11, 2:40 pm, Darren <Dar...@nospam.nospamwrote:
if ( control is ICustomControl )
{
....

}
Ron wrote:
I have a situation where I need to test if a Control implements from a
specific Interface to avoid an invalid cast exception:
foreach (Control ctrl in this.Controls)
{
// Need to test to see if ctrl inherits interface ICustomControl here
ICustomControl control = (ICustomControl)ctrl;
}
Thanks!
Ron
Alternately, he can do this:
foreach (Control ctrl in this.Controls)
{
// Need to test to see if ctrl inherits interface ICustomControl here
ICustomControl control = ctrl as ICustomControl;
if ( control != null ) {
// do something
}
}
Mar 11 '08 #4

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

Similar topics

15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
11
by: Antony | last post by:
I know this sounds stupid but I am going to carry on anyway. I want to create an interface that implements all methods of a form, plus another one or two. But I need to know if there is an...
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
2
by: Don | last post by:
1. Say I have three classes: A, B and C. 2. Class A can only be inherited, and B inherits it. C does not inherit anything. 3. I have a function that can have an object passed to it. Either B...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
1
by: Mike9900 | last post by:
I would like to return an object which inherits an interface. A web service instantiate a class that inherits that interface and returns that object. But I get error when referencing the web...
1
by: Lucile | last post by:
Hi to All, I am new to this newsgroup, so please bare with me. Here is the deal: I have the following Object Graph: Icell (Interface) Iline (Interface) Itable (Interface) Classes: Cell:...
0
by: asumasters | last post by:
Hi, I am having a problem with interface type. Can anyone please help me in this regard. I have been working on trying to invoke the Intusoft simulator using C# windows application. I am...
8
by: carton | last post by:
Any opinions on this code as part of a general purpose framework? I expect people will have varying opinions on the use of $ in the property names. I did it for several reasons: 'interface' is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.