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

Does a class implement an interface?

interface Interface1
{
}

interface Interface2 : Interface1
{
}

class A : Interface2
{
}
static void Main(string[] args)
{
Console.WriteLine(typeof(A).IsSubclassOf(typeof(In terface1)).ToString());
Console.WriteLine(typeof(A).IsSubclassOf(typeof(In terface2)).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

Oct 20 '08 #1
7 1657
Peter:
Try using the method " typeof(Page).IsAssignableFrom(

Gustavo A. Cantero
CEO - Scientia® Soluciones Informáticas
MCP - MCSD - MCTS
http://www.scientia.com.ar
http://www.programandoamedianoche.com
http://foro.scientia.com.ar

-----Mensaje original-----
De: Peter Morris [mailto:mr*********@SPAMgmail.com]
Expuesto a las: Lunes, 20 de Octubre de 2008 12:06 p.m.
Expuesto en: microsoft.public.dotnet.languages.csharp
Conversación: Does a class implement an interface?
Asunto: Does a class implement an interface?

interface Interface1
{
}

interface Interface2 : Interface1
{
}

class A : Interface2
{
}
static void Main(string[] args)
{

Console.WriteLine(typeof(A).IsSubclassOf(typeof(In terface1)).ToString())
;

Console.WriteLine(typeof(A).IsSubclassOf(typeof(In terface2)).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

Oct 20 '08 #2
Peter:
Use the method "IsAssignableFrom", like this:

interface Interface1
{
}

interface Interface2 : Interface1
{
}

class A : Interface2
{
}
static void Main(string[] args)
{

Console.WriteLine(typeof(A).IsAssignableFrom(typeo f(Interface1)).ToStrin
g());

Console.WriteLine(typeof(A).IsAssignableFrom(typeo f(Interface2)).ToStrin
g());
Console.ReadLine();
}

Good luck!

Gustavo A. Cantero
CEO - Scientia® Soluciones Informáticas
MCP - MCSD - MCTS
http://www.scientia.com.ar
http://www.programandoamedianoche.com
http://foro.scientia.com.ar

-----Mensaje original-----
De: Peter Morris [mailto:mr*********@SPAMgmail.com]
Expuesto a las: Lunes, 20 de Octubre de 2008 12:06 p.m.
Expuesto en: microsoft.public.dotnet.languages.csharp
Conversación: Does a class implement an interface?
Asunto: Does a class implement an interface?

interface Interface1
{
}

interface Interface2 : Interface1
{
}

class A : Interface2
{
}
static void Main(string[] args)
{

Console.WriteLine(typeof(A).IsSubclassOf(typeof(In terface1)).ToString())
;

Console.WriteLine(typeof(A).IsSubclassOf(typeof(In terface2)).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

Oct 20 '08 #3
Peter:
Try using the method "IsAssignableFrom", like this:

interface Interface1
{
}

interface Interface2 : Interface1
{
}

class A : Interface2
{
}
static void Main(string[] args)
{

Console.WriteLine(typeof(A).IsAssignableFrom(typeo f(Interface1)).ToStrin
g());

Console.WriteLine(typeof(A).IsAssignableFrom(typeo f(Interface2)).ToStrin
g());
Console.ReadLine();
}

Good luck!

Gustavo A. Cantero
CEO - Scientia® Soluciones Informáticas
MCP - MCSD - MCTS
http://www.scientia.com.ar
http://www.programandoamedianoche.com
http://foro.scientia.com.ar

-----Mensaje original-----
De: Peter Morris [mailto:mr*********@SPAMgmail.com]
Expuesto a las: Lunes, 20 de Octubre de 2008 12:06 p.m.
Expuesto en: microsoft.public.dotnet.languages.csharp
Conversación: Does a class implement an interface?
Asunto: Does a class implement an interface?

interface Interface1
{
}

interface Interface2 : Interface1
{
}

class A : Interface2
{
}
static void Main(string[] args)
{

Console.WriteLine(typeof(A).IsSubclassOf(typeof(In terface1)).ToString())
;

Console.WriteLine(typeof(A).IsSubclassOf(typeof(In terface2)).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

Oct 20 '08 #4
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("Interfac e1").Name ==
typeof(Interface1).Name);
Console.ReadLine();
}

--
Thank you,

Christopher Ireland

Oct 20 '08 #5
Console.WriteLine(typeof(A).IsAssignableFrom(typeo f(Interface1)).ToString());
Console.WriteLine(typeof(A).IsAssignableFrom(typeo f(Interface2)).ToString());
Console.ReadLine();

OUTPUT:
False
False

Any other ideas?

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

Oct 20 '08 #6
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

Oct 20 '08 #7
Peter Morris wrote:
>
Console.WriteLine(typeof(A).IsAssignableFrom(typeo f(Interface1)).ToString());
Console.WriteLine(typeof(A).IsAssignableFrom(typeo f(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.
Oct 20 '08 #8

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

Similar topics

15
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
5
by: HAM | last post by:
One of my friends asked if the followings have any meanings? '------------- Public Interface IRenderable Sub Render() MustInherit Class Engine MustOverride Sub TurnOn() Interface IAutomatic...
4
by: Sanjay Vyas | last post by:
Sorry, forgot to cross post this one.. This is rather unusual as we would expect any Collection class to implement ICollection interface and furthermore a Dictionary class should implement...
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
5
by: g18c | last post by:
Hi, im trying to do something and just been caught off guard. I want to implement an interface but only for certain class, but of course when i derive from this base class the derived class also...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
9
by: Jonathan Wood | last post by:
I found the following class on the Web: public class LoginRewriter : IHttpModule { void IHttpModule.Dispose() { } void IHttpModule.Init(HttpApplication app) { app.AuthorizeRequest += new...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
3
by: =?Utf-8?B?Sm9uIEU=?= | last post by:
I have an interface class with maybe eight functions, defined in one workspace and am defining a class in a second workspace that derives from this interface. Unfortunately only 7 of the 8...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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.