473,769 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to determine base interface???

Suppose the following:

class MyContainer : System.Collecti ons.CollectionB ase
{
//...
}

(where CollectionBase implements IList, ICollection)

How do I determine if a type (such as MyContainer) derives from IList?

System.Type type = typeof(MyContai ner);

type is IList -> false

type.IsSubclass Of(typeof(IList )) -> false

type.IsAssignab leFrom(typeof(I List)) -> false

etc., all tests return false.

So, what I need is a way to determine if a particular type derives from IList,
no matter how far up the hierarchy it is.

Thanks
Nov 16 '05 #1
14 24434
Hi!

how about that ? is that too much overkill ?
//***
IList lst = type as IList;
if (lst != null)
Console.WriteLi ne("type does implement IList");
//***

--
Best Regards
Yanick
"J. Jones" <jj@networld.co m> a écrit dans le message de
news:e2******** ******@TK2MSFTN GP14.phx.gbl...
Suppose the following:

class MyContainer : System.Collecti ons.CollectionB ase
{
//...
}

(where CollectionBase implements IList, ICollection)

How do I determine if a type (such as MyContainer) derives from IList?

System.Type type = typeof(MyContai ner);

type is IList -> false

type.IsSubclass Of(typeof(IList )) -> false

type.IsAssignab leFrom(typeof(I List)) -> false

etc., all tests return false.

So, what I need is a way to determine if a particular type derives from IList, no matter how far up the hierarchy it is.

Thanks

Nov 16 '05 #2
J. Jones wrote:
How do I determine if a type (such as MyContainer) derives from IList?

Use the C# is statement:
if (MyContainer is IList) {
// Handle IList implementation
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #3
Zoury wrote:
Hi!

how about that ? is that too much overkill ?
?!
//***
IList lst = type as IList;
if (lst != null)
Console.WriteLi ne("type does implement IList");
//***


No go. IList isn't a type, but a class (interface). You can't convert a type
to an instance, what is would happen if permissible in your code.
Nov 16 '05 #4
Anders Norås [MCAD] wrote:
J. Jones wrote:
How do I determine if a type (such as MyContainer) derives from IList?


Use the C# is statement:
if (MyContainer is IList) {
// Handle IList implementation
}


No go. I need to test a System.Type variable, so your code becomes something like:

if (typeof(MyConta iner) is IList)

which is false.

Thanks
Nov 16 '05 #5
Anders Norås [MCAD] wrote:
J. Jones wrote:
How do I determine if a type (such as MyContainer) derives from IList?


Use the C# is statement:
if (MyContainer is IList) {
// Handle IList implementation
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/


MyContainer is a class, not a variable, so that won't compile.

This is ugly, but it works:

using System;

interface IWhatever {}

public class App : IWhatever
{
public static void Main()
{
bool implementsIFace =
ImplementsIFace (typeof(IClonea ble), typeof(App));
}

static bool ImplementsIFace (Type interfaceType, Type implementingTyp e)
{
Type[] interfaces = implementingTyp e.GetInterfaces ();
foreach(Type t in interfaces)
{
if(t == interfaceType) return true;
}
return false;
}
}

/Joakim
Nov 16 '05 #6
oh sorry i did misread your sample.

but i've noticed though that you misread the help file, since the
BaseCollection *does not* implements the Ilist :
---
Public Class BaseCollection
Inherits MarshalByRefObj ect
Implements ICollection, IEnumerable
---

so here's how you could test it (without any loop) (not tested) :
'***
Console.WriteLi ne("MyContaine r implements IEnumerable : {0}",
typeof(MyContai ner).GetInterfa ce("System.Coll ections.IEnumer able") != null)
'***

--
Best Regards
Yanick
Nov 16 '05 #7
"Anders Norås [MCAD]" <an**********@o bjectware.no> wrote:
J. Jones wrote:
How do I determine if a type (such as MyContainer) derives from IList?

Use the C# is statement:
if (MyContainer is IList) {
// Handle IList implementation
}


I believe (given the rest of the post) that the OP is trying to get
that information from the Type reference, not an instance of the type.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
<"Zoury" <yanick_lefebvr e at hotmail dot com>> wrote:
how about that ? is that too much overkill ?
//***
IList lst = type as IList;
if (lst != null)
Console.WriteLi ne("type does implement IList");
//***


It still won't work, for the same reason that "type is IList" won't
work - the Type type itself doesn't implement IList.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
type.IsAssigna bleFrom(typeof( IList)) -> false


Close, but backwards. Try

typeof(IList).I sAssignableFrom (type)


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #10

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

Similar topics

22
5838
by: Marcin Vorbrodt | last post by:
Taken out of C++ In a Nutshell book... Example 7-18: Using an abstract classes as interface specification. struct Runnable { virtual void run() = 0; }; struct Hashable { virtual size_t hash() = 0; };
24
2362
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no virtual funcitons have no rtti) That is, there is no way to do something like: try{ funct_from_3rd_party(); } catch(...){ std:err << extract_name() << std::endl; }
1
1850
by: Matthias Kaeppler | last post by:
Sorry if this has been discussed before (I'm almost certain it has), but I didn't know what to google for. My problem is, I have a class, a gtkmm widget, and I want it to serve as a base class now, but I'm not sure if I'm taking the proper steps in order to not break the whole class. Are there any guidelines what I have to watch out for? One question would e.g. be:
15
12791
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } class MyClass :ThirdPartyClass, IDisposable { void IDisposable.Dispose() {
4
2857
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. '**************************************************************************** ' Issues '****************************************************************************
2
1789
by: Oenone | last post by:
I could use a little advice to help prevent me making a possible mess of a project. :) In VB6, I once created a project that exposed a public interface class. I then Implemented this in various plug-in DLLs so that I could early-bind to the plug-ins by declaring objects of the interface class type. This worked fine, until one day I found that I needed to add a new method to the interface class. Of course, everything broke immediately...
9
5200
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
6
2844
by: noel.hunt | last post by:
I have a base class, PadRcv, with virtual functions. User code will derive from this class and possibly supply it's own functions to override the base class virtual functions. How can I test that a user-defined class has overriden the virtual function in the base class? I have code like this which compiles with gcc 2.95.4: Attrib Implicits(PadRcv *obj){ Attrib accum = 0;
2
2412
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10205
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
10035
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...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6662
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
5293
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...
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
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.