473,796 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does a class implement an interface?

interface Interface1
{
}

interface Interface2 : Interface1
{
}

class A : Interface2
{
}
static void Main(string[] args)
{
Console.WriteLi ne(typeof(A).Is SubclassOf(type of(Interface1)) .ToString());
Console.WriteLi ne(typeof(A).Is SubclassOf(type of(Interface2)) .ToString());
Console.ReadLin e();
}
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 1684
Peter:
Try using the method " typeof(Page).Is AssignableFrom(

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.c om]
Expuesto a las: Lunes, 20 de Octubre de 2008 12:06 p.m.
Expuesto en: microsoft.publi c.dotnet.langua ges.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.WriteLi ne(typeof(A).Is SubclassOf(type of(Interface1)) .ToString())
;

Console.WriteLi ne(typeof(A).Is SubclassOf(type of(Interface2)) .ToString())
;
Console.ReadLin e();
}
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 "IsAssignableFr om", like this:

interface Interface1
{
}

interface Interface2 : Interface1
{
}

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

Console.WriteLi ne(typeof(A).Is AssignableFrom( typeof(Interfac e1)).ToStrin
g());

Console.WriteLi ne(typeof(A).Is AssignableFrom( typeof(Interfac e2)).ToStrin
g());
Console.ReadLin e();
}

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.c om]
Expuesto a las: Lunes, 20 de Octubre de 2008 12:06 p.m.
Expuesto en: microsoft.publi c.dotnet.langua ges.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.WriteLi ne(typeof(A).Is SubclassOf(type of(Interface1)) .ToString())
;

Console.WriteLi ne(typeof(A).Is SubclassOf(type of(Interface2)) .ToString())
;
Console.ReadLin e();
}
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 "IsAssignableFr om", like this:

interface Interface1
{
}

interface Interface2 : Interface1
{
}

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

Console.WriteLi ne(typeof(A).Is AssignableFrom( typeof(Interfac e1)).ToStrin
g());

Console.WriteLi ne(typeof(A).Is AssignableFrom( typeof(Interfac e2)).ToStrin
g());
Console.ReadLin e();
}

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.c om]
Expuesto a las: Lunes, 20 de Octubre de 2008 12:06 p.m.
Expuesto en: microsoft.publi c.dotnet.langua ges.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.WriteLi ne(typeof(A).Is SubclassOf(type of(Interface1)) .ToString())
;

Console.WriteLi ne(typeof(A).Is SubclassOf(type of(Interface2)) .ToString())
;
Console.ReadLin e();
}
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.WriteLi ne(typeof(A).Ge tInterface("Int erface1").Name ==
typeof(Interfac e1).Name);
Console.ReadLin e();
}

--
Thank you,

Christopher Ireland

Oct 20 '08 #5
Console.WriteLi ne(typeof(A).Is AssignableFrom( typeof(Interfac e1)).ToString() );
Console.WriteLi ne(typeof(A).Is AssignableFrom( typeof(Interfac e2)).ToString() );
Console.ReadLin e();

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(IInterfa ce1).IsAssignab leFrom(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.WriteLi ne(typeof(A).Is AssignableFrom( typeof(Interfac e1)).ToString() );
Console.WriteLi ne(typeof(A).Is AssignableFrom( typeof(Interfac e2)).ToString() );
Console.ReadLin e();
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
2416
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 inheriting this base usercontrol to override the method with its own code. How do I do it? I have tried to make the base usercontrol an abstract class (mustinherits + mustoverrides), this works, but all the usercontrols that inherit it will not able...
5
1552
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 Sub Start() End Interface
4
3714
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 IDictionary interface. The StringDictionary class does implements all the methods of ICollection and IDictionary yet it does not list these interfaces. Am I missing something here or was it an oversight on the part of the class developer. Maybe I have...
9
7559
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 be implemented. So if you have a class which inherits base class that inherts an interface, then your classes will have a standard. I suppose you can also check for interface at run time say when dll is loaded and see if it implememts whats...
5
1650
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 inherits the interface. public class Base : IGetType { protected byte m_type; object IGetType.TypeIdentifier
52
20913
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 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
9
1966
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 EventHandler(authorizeRequest); }
162
10309
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 prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
3
15755
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 functions in my derived class compile whilst just one of them refuses to be recognised, leading to an error message "...does not implement interface member...". Try as I might, I can't fix the error. I have cut and paste names so can rule out simple...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.