Skip to content

How To Determine if Object Derives From Collection Type

Ever wanted to know how to determine if an object derives or implements from a collection type?

I figured as all collections I understand in .NET 2.0+ derive unequivocally back to IEnumerable I could test if my object implemented this class.

However after trying a few of my own ideas (the obvious Obj is IEnumerable, Obj is IList), looking around and then asking on Stack Overflow, I could not find an answer and it did not seem as simple as I thought! I did receive a few answers though where people suggested to test implementation of IEnumerable, but none of the code examples seemed to work for me to test a Generic.List of mine with a set of custom objects.

However a late post from s_ruchit returned something so simple:

if(Obj is ICollection)
{
    //Derived from ICollection
}
else
{
    //Not Derived from ICollection
}

Now I’ve seen that, I would prefer to use IList (obj is IList), as I know it is more applicable to the type I wish to test. But any ideas why my Obj is IEnumerable and Obj is IList did not work for my System.Collections.Generic.List? Very interesting! I’d be happy to hear thoughts. (not like Sylar from Heroes, but I mean.. comments on here.)

VN:F [1.8.3_1051]
Rating: 0.0/5 (0 votes cast)
VN:F [1.8.3_1051]
Rating: 0 (from 0 votes)
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

4 Comments

  1. if (obj is IList) will call System.Collections.IList and NOT System.Collections.Generic.IList, because you’ll need to provide the type parameter for the generic, i.e.

    if (obj is IList)

    If that makes sense? :)

    Monty

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 9:10 am | Permalink
  2. Wordpress has just taken out my type parameters, trying again:

    if (obj is IList[String])

    Instead of [, use angle brackets, you know what I mean :)

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 9:12 am | Permalink
  3. Graham O'Neale

    @Monty: Yeah you’d probably have to use & lt; without the space to make that work as proper < and >.

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 10:07 am | Permalink
  4. Graham O'Neale

    @Monty: Hmmm. Well I never know the type, as it was a custom class coming in as “T”, so I don’t know if IList<T> would have worked either. Ah well, past that now, thanks for your comments for the next intrepid adventures to come stumbling by here…. =)

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 10:10 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*
My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...