Connect with Expertise | Find Experts, Get Answers, Share Insights

How to determine base interface???

J. Jones
 
Posts: n/a
#1: Nov 16 '05
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

Zoury
 
Posts: n/a
#2: Nov 16 '05

re: How to determine base interface???


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:e2ExrjO9EHA.2124@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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[/color]
IList,[color=blue]
> no matter how far up the hierarchy it is.
>
> Thanks[/color]


Anders Norås [MCAD]
 
Posts: n/a
#3: Nov 16 '05

re: How to determine base interface???


J. Jones wrote:[color=blue]
> How do I determine if a type (such as MyContainer) derives from IList?[/color]
Use the C# is statement:
if (MyContainer is IList) {
// Handle IList implementation
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
J. Jones
 
Posts: n/a
#4: Nov 16 '05

re: How to determine base interface???


Zoury wrote:[color=blue]
> Hi!
>
> how about that ? is that too much overkill ?[/color]

?!
[color=blue]
> //***
> IList lst = type as IList;
> if (lst != null)
> Console.WriteLine("type does implement IList");
> //***
>[/color]

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. Jones
 
Posts: n/a
#5: Nov 16 '05

re: How to determine base interface???


Anders Norås [MCAD] wrote:[color=blue]
> J. Jones wrote:
>[color=green]
>> How do I determine if a type (such as MyContainer) derives from IList?[/color]
>
> Use the C# is statement:
> if (MyContainer is IList) {
> // Handle IList implementation
> }[/color]

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
Joakim Karlsson
 
Posts: n/a
#6: Nov 16 '05

re: How to determine base interface???


Anders Norås [MCAD] wrote:[color=blue]
> J. Jones wrote:
>[color=green]
>> How do I determine if a type (such as MyContainer) derives from IList?[/color]
>
> Use the C# is statement:
> if (MyContainer is IList) {
> // Handle IList implementation
> }
>
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras/[/color]

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
Zoury
 
Posts: n/a
#7: Nov 16 '05

re: How to determine base interface???


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


Jon Skeet [C# MVP]
 
Posts: n/a
#8: Nov 16 '05

re: How to determine base interface???


"Anders Norås [MCAD]" <anders.noras@objectware.no> wrote:[color=blue]
> J. Jones wrote:[color=green]
> > How do I determine if a type (such as MyContainer) derives from IList?[/color]
> Use the C# is statement:
> if (MyContainer is IList) {
> // Handle IList implementation
> }[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP]
 
Posts: n/a
#9: Nov 16 '05

re: How to determine base interface???


<"Zoury" <yanick_lefebvre at hotmail dot com>> wrote:[color=blue]
> how about that ? is that too much overkill ?
> //***
> IList lst = type as IList;
> if (lst != null)
> Console.WriteLine("type does implement IList");
> //***[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Mattias Sjögren
 
Posts: n/a
#10: Nov 16 '05

re: How to determine base interface???


[color=blue]
>type.IsAssignableFrom(typeof(IList)) -> false[/color]

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.
Jon Skeet [C# MVP]
 
Posts: n/a
#11: Nov 16 '05

re: How to determine base interface???


J. Jones <jj@networld.com> wrote:[color=blue]
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Anders Norås [MCAD]
 
Posts: n/a
#12: Nov 16 '05

re: How to determine base interface???


J. Jones wrote:[color=blue]
> No go. I need to test a System.Type variable, so your code becomes[/color]
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/
Joakim Karlsson
 
Posts: n/a
#13: Nov 16 '05

re: How to determine base interface???


Zoury wrote:[color=blue]
> Console.WriteLine("MyContainer implements IEnumerable : {0}",
> typeof(MyContainer).GetInterface("System.Collectio ns.IEnumerable") != null)[/color]

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
Zoury
 
Posts: n/a
#14: Nov 16 '05

re: How to determine base interface???


> but i've noticed though that you misread the help file, since the[color=blue]
> BaseCollection *does not* implements the Ilist :[/color]

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


J. Jones
 
Posts: n/a
#15: Nov 16 '05

re: How to determine base interface???


Jon Skeet [C# MVP] wrote:[color=blue]
> J. Jones <jj@networld.com> wrote:
>[color=green]
>>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.[/color]
>
>
> 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)
>[/color]

Check! Switched, worked as desired.

Thanks.
Closed Thread