How to determine base interface???

J

J. Jones

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
 
Z

Zoury

Hi!

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

J. Jones

Zoury said:
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.
 
J

J. Jones

Anders said:
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
 
J

Joakim Karlsson

Anders said:
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
 
Z

Zoury

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.Collections.IEnumerable") != null)
'***
 
J

Jon Skeet [C# MVP]

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.
 
J

Jon Skeet [C# MVP]

Anders Norås said:
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.
 
J

Jon Skeet [C# MVP]

J. Jones said:
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)
 
?

=?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=

J. Jones said:
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).FullName)) {
// MyContainer implements IList.
} else {
// MyContainer does not implement IList.
}

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

Joakim Karlsson

Zoury said:
Console.WriteLine("MyContainer implements IEnumerable : {0}",
typeof(MyContainer).GetInterface("System.Collections.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
 
Z

Zoury

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)
 
J

J. Jones

Jon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top