469,903 Members | 1,517 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

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 1548
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

52 posts views Thread by Ben Voigt [C++ MVP] | last post: by
9 posts views Thread by Jonathan Wood | last post: by
3 posts views Thread by =?Utf-8?B?Sm9uIEU=?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.