PC Review


Reply
Thread Tools Rate Thread

How to determine base interface???

 
 
J. Jones
Guest
Posts: n/a
 
      7th Jan 2005
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
 
Reply With Quote
 
 
 
 
Zoury
Guest
Posts: n/a
 
      7th Jan 2005
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" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
=?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=
Guest
Posts: n/a
 
      7th Jan 2005
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/
 
Reply With Quote
 
J. Jones
Guest
Posts: n/a
 
      7th Jan 2005
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.
 
Reply With Quote
 
J. Jones
Guest
Posts: n/a
 
      7th Jan 2005
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
 
Reply With Quote
 
Joakim Karlsson
Guest
Posts: n/a
 
      7th Jan 2005
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
 
Reply With Quote
 
Zoury
Guest
Posts: n/a
 
      7th Jan 2005
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)
'***

--
Best Regards
Yanick


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Jan 2005
<"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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Jan 2005
"Anders Norås [MCAD]" <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Jan 2005
J. Jones <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to determine the base URL of current page Anonieko Microsoft ASP .NET 8 4th Jun 2007 01:47 AM
How to determine implemented interface Tosch Microsoft VB .NET 2 19th May 2005 02:58 PM
dynamically determine the base href value Guoqi Zheng Microsoft ASP .NET 3 13th Jan 2005 05:33 PM
Determine which assembly references are not from the base framework... Nathan Baulch Microsoft Dot NET Framework 3 27th Oct 2004 11:18 AM
Reflection--can I determine if an interface was inherited without checking the base class? Jeff Johnson [MVP: VB] Microsoft Dot NET Framework 1 12th May 2004 08:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:44 PM.