PC Review


Reply
Thread Tools Rate Thread

Does a class implement an interface?

 
 
Peter Morris
Guest
Posts: n/a
 
      20th Oct 2008
interface Interface1
{
}

interface Interface2 : Interface1
{
}

class A : Interface2
{
}


static void Main(string[] args)
{
Console.WriteLine(typeof(A).IsSubclassOf(typeof(Interface1)).ToString());
Console.WriteLine(typeof(A).IsSubclassOf(typeof(Interface2)).ToString());
Console.ReadLine();
}


Both return False. Given a type what is the simplest way of checking if it
implements Interface1 either directly, or by implementing Interface2?




--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

 
Reply With Quote
 
 
 
 
Christopher Ireland
Guest
Posts: n/a
 
      20th Oct 2008
Peter Morris wrote:

> Both return False. Given a type what is the simplest way of checking
> if it implements Interface1 either directly, or by implementing
> Interface2?


How about GetInterface(), e.g.

static void Main(string[] args)
{
Console.WriteLine(typeof(A).GetInterface("Interface1").Name ==
typeof(Interface1).Name);
Console.ReadLine();
}

--
Thank you,

Christopher Ireland

 
Reply With Quote
 
Peter Morris
Guest
Posts: n/a
 
      20th Oct 2008
Console.WriteLine(typeof(A).IsAssignableFrom(typeof(Interface1)).ToString());
Console.WriteLine(typeof(A).IsAssignableFrom(typeof(Interface2)).ToString());
Console.ReadLine();

OUTPUT:
False
False

Any other ideas?



--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

 
Reply With Quote
 
Peter Morris
Guest
Posts: n/a
 
      20th Oct 2008
Obvious when you know how!

typeof(IInterface1).IsAssignableFrom(typeof(A));

If A implements IInterface1 then it means A can be assigned to IInterface1,
not that IInterface1 can be assigned to A!



--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      20th Oct 2008
Peter Morris wrote:
>
> Console.WriteLine(typeof(A).IsAssignableFrom(typeof(Interface1)).ToString());
> Console.WriteLine(typeof(A).IsAssignableFrom(typeof(Interface2)).ToString());
> Console.ReadLine();
> OUTPUT:
> False
> False
>
> Any other ideas?


Turn the logic around, the interface is assignable from the class (upcast)
but the reverse (downcast) is not true.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
abstract class 'does not implement interface member ...' Ben Voigt [C++ MVP] Microsoft C# .NET 52 25th Jun 2007 02:13 PM
Implement interface for that class, not dervived classes g18c@hotmail.com Microsoft C# .NET 5 5th Feb 2007 01:00 PM
Can a base class implement a method on an interface for me? MrAnon Microsoft VB .NET 3 16th Nov 2006 04:47 PM
How to implement interface in Managed class ??? =?Utf-8?B?aHVsaW5uaW5n?= Microsoft Dot NET Framework 0 24th May 2006 06:21 PM
extend class and implement interface farseer Microsoft C# .NET 5 17th Oct 2005 08:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:54 PM.