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

How to determine base interface???

Suppose the following:

class MyContainer : System.Collections.CollectionBase
{
//...
}

(where CollectionBase implements IList, ICollection)

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

System.Type type = typeof(MyContainer);

type is IList -> false

type.IsSubclassOf(typeof(IList)) -> false

type.IsAssignableFrom(typeof(IList)) -> 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 24409
Hi!

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

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

class MyContainer : System.Collections.CollectionBase
{
//...
}

(where CollectionBase implements IList, ICollection)

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

System.Type type = typeof(MyContainer);

type is IList -> false

type.IsSubclassOf(typeof(IList)) -> false

type.IsAssignableFrom(typeof(IList)) -> 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.WriteLine("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(MyContainer) 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(ICloneable), typeof(App));
}

static bool ImplementsIFace(Type interfaceType, Type implementingType)
{
Type[] interfaces = implementingType.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 MarshalByRefObject
Implements ICollection, IEnumerable
---

so here's how you could test it (without any loop) (not tested) :
'***
Console.WriteLine("MyContainer implements IEnumerable : {0}",
typeof(MyContainer).GetInterface("System.Collectio ns.IEnumerable") != null)
'***

--
Best Regards
Yanick
Nov 16 '05 #7
"Anders Norås [MCAD]" <an**********@objectware.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
<"Zoury" <yanick_lefebvre at hotmail dot com>> wrote:
how about that ? is that too much overkill ?
//***
IList lst = type as IList;
if (lst != null)
Console.WriteLine("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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
type.IsAssignableFrom(typeof(IList)) -> false


Close, but backwards. Try

typeof(IList).IsAssignableFrom(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
J. Jones <jj@networld.com> wrote:
Suppose the following:

class MyContainer : System.Collections.CollectionBase
{
//...
}

(where CollectionBase implements IList, ICollection)

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

System.Type type = typeof(MyContainer);

type is IList -> false

type.IsSubclassOf(typeof(IList)) -> false

type.IsAssignableFrom(typeof(IList)) -> 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.


You're using IsAssignableFrom the wrong way round - it's very easy to
do, and I always have to look up the documentation to check it.
Basically, you want

typeof(IList).IsAssignableFrom(type)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
J. Jones wrote:
No go. I need to test a System.Type variable, so your code becomes

Ops. My mistake, I read the question a bit too fast and though
MyContainer was a variable. If MyContainer is a class you can do this.

if (typeof(MyContainer).GetInterface(typeof(IList).Fu llName)) {
// MyContainer implements IList.
} else {
// MyContainer does not implement IList.
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #12
Zoury wrote:
Console.WriteLine("MyContainer implements IEnumerable : {0}",
typeof(MyContainer).GetInterface("System.Collectio ns.IEnumerable") != null)


Doh! Of course that how it is done! For some reason I got into my head
that Type.GetInterface() would throw an exception if the requested
interface wasn't implemented.

Time to log off I guess! :)

Thanks Zoury!

/Joakim
Nov 16 '05 #13
> but i've noticed though that you misread the help file, since the
BaseCollection *does not* implements the Ilist :


man... it's for me to take off.
you were talking about CollectionBase and not BaseCollection....

anyway the sample pattern works properly. But as shown by Mattias,
IsAssignableFrom() seems to be the proper way to do it.

have a nice weekend !
i know will :O)

--
Best Regards
Yanick
Nov 16 '05 #14
Jon Skeet [C# MVP] wrote:
J. Jones <jj@networld.com> wrote:
Suppose the following:

class MyContainer : System.Collections.CollectionBase
{
//...
}

(where CollectionBase implements IList, ICollection)

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

System.Type type = typeof(MyContainer);

type is IList -> false

type.IsSubclassOf(typeof(IList)) -> false

type.IsAssignableFrom(typeof(IList)) -> 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.

You're using IsAssignableFrom the wrong way round - it's very easy to
do, and I always have to look up the documentation to check it.
Basically, you want

typeof(IList).IsAssignableFrom(type)


Check! Switched, worked as desired.

Thanks.
Nov 16 '05 #15

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

Similar topics

22
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...
24
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...
1
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...
15
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" ); } } ...
4
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. ...
2
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...
9
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
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...
2
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...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.