By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,675 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,675 IT Pros & Developers. It's quick & easy.

How to determine base interface???

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
<"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.