How to determine base interface???

  • Thread starter Thread starter J. Jones
  • Start date Start date
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
 
Hi!

how about that ? is that too much overkill ?
//***
IList lst = type as IList;
if (lst != null)
Console.WriteLine("type does implement IList");
//***
 
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.
 
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
 
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
 
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)
'***
 
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.
 
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. 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)
 
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/
 
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
 
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)
 
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.
 
Back
Top