[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Does Object Implement an Interface?

coconet

10/27/2008 7:11:00 PM



How can I tell if an object I have reference to implements a
particular interface?

class foo : Form
{
somemethod()
{
this.Controls.Add( new bar() );
}
}

class bar : Panel
{
public void LookParent()
{
if ( this.parentForm.GetType().ImplementsAnInterface( IMine) )
{
string ok = "iok";
}
}
}

As far as I know there is no method called "ImplementsAnInterface", so
how can I make one?

Thanks.

2 Answers

Peter Duniho

10/27/2008 7:23:00 PM

0

On Mon, 27 Oct 2008 12:11:09 -0700, Co Co Net <coconet@community.nospam>
wrote:

> [...]
> class bar : Panel
> {
> public void LookParent()
> {
> if ( this.parentForm.GetType().ImplementsAnInterface( IMine) )
> {
> string ok = "iok";
> }
> }
> }
>
> As far as I know there is no method called "ImplementsAnInterface", so
> how can I make one?

Use the "is" operator:

if (this.parentForm is IMine)
{
string ok = "iok";
}

There is in fact a method on the Type class that returns similar
information, but if you have an instance, you don't need it.

Pete

Jack Jackson

10/27/2008 7:59:00 PM

0

On Mon, 27 Oct 2008 15:11:09 -0400, Co Co Net
<coconet@community.nospam> wrote:

>
>
>How can I tell if an object I have reference to implements a
>particular interface?
>
>class foo : Form
>{
> somemethod()
> {
> this.Controls.Add( new bar() );
> }
>}
>
>class bar : Panel
>{
> public void LookParent()
> {
> if ( this.parentForm.GetType().ImplementsAnInterface( IMine) )
> {
> string ok = "iok";
> }
> }
>}
>
>As far as I know there is no method called "ImplementsAnInterface", so
>how can I make one?

Google of ".NET does object implement interface" returns a lot of
hits. The first one is:

<http://bytes.com/forum/thread38596...