(inheritance) problem with generic

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I try to test the interface implemented by a generic with the simple test
below.
What puzzle me is that all 4 test return false!
Any idea why is that and how to find the IList interface from the type?

-------------
using System;
using System.Collections;
using System.Collections.Generic;

// csc /nologo Class1.cs && Class1
public class Class1
{
public static void Main()
{
List<string> ls = new List<string>();
if(ls is IList)
{
Console.WriteLine("is IList(1): " +
ls.GetType().IsSubclassOf(typeof(IList)));
Console.WriteLine("is IList(2): " +
ls.GetType().IsAssignableFrom(typeof(IList)));
Console.WriteLine("is IList(3): " +
ls.GetType().IsInstanceOfType(typeof(IList)));
Console.WriteLine("is IList(4): " +
ls.GetType().IsDefined(typeof(IList), true));
}
}
}
 
Have you tried "Console.WriteLine(ls is IList)"?

If ls is castable to IList, the result of using "is" operator will be "true"
 
obviously you haven't looked at my example....

yep what you worte (and I wrote it too), works very well.
however when you have a Type object, it's a bit more difficult with generic.

Anyway I solved it and posted the solution
 
Console.WriteLine("is IList(2): " +
ls.GetType().IsAssignableFrom(typeof(IList)));

You're using it the wrong way around, try

Console.WriteLine("is IList(2): " +
typeof(IList).IsAssignableFrom(ls.GetType()));



Mattias
 
ooh...
aheum, indeed.....
thanks!

Mattias Sjögren said:
You're using it the wrong way around, try

Console.WriteLine("is IList(2): " +
typeof(IList).IsAssignableFrom(ls.GetType()));



Mattias
 

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

Back
Top