Generics and casting - Cant cast IList<MyType> to IList<object>

K

KC

Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the following error:

Argument '1': cannot convert from 'GenericsPOC.ArticleCollection' to 'System.Collections.Generic.IList<object>'

I tried casting specifically to IList<object> but then I get a runtime error:
Unable to cast object of type 'GenericsPOC.ArticleCollection' to type 'System.Collections.Generic.IList`1'.
class Program
{
static void Main(string[] args)
{
ArticleCollection ac = new ArticleCollection();
Mapper.MapObject(ac);
Mapper.MapObject((IList<object>)ac); //Another attempt
}
}

public class Article
{
}

public class ArticleCollection : List<Article>
{
}

public class Mapper
{
public static object MapObject(IList<object> list)
{
return null;
}

public static IList<object> MapObject(List<object> list)
{
return list;
}

}
 
L

Lebesgue

Just letting you know, there is also a group called microsoft.private.whidbey.generics
a.. Server: privatenews.microsoft.com
b.. Account name: privatenews\VSUser
c.. Password: Password
d.. Note that the password is case-sensitive.
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the following error:

Argument '1': cannot convert from 'GenericsPOC.ArticleCollection' to 'System.Collections.Generic.IList<object>'

I tried casting specifically to IList<object> but then I get a runtime error:
Unable to cast object of type 'GenericsPOC.ArticleCollection' to type 'System.Collections.Generic.IList`1'.
class Program
{
static void Main(string[] args)
{
ArticleCollection ac = new ArticleCollection();
Mapper.MapObject(ac);
Mapper.MapObject((IList<object>)ac); //Another attempt
}
}

public class Article
{
}

public class ArticleCollection : List<Article>
{
}

public class Mapper
{
public static object MapObject(IList<object> list)
{
return null;
}

public static IList<object> MapObject(List<object> list)
{
return list;
}

}
 
J

Jon Skeet [C# MVP]

KC said:
Could some one explain to me the casting rules for sending generic
lists, ex. List<Person>, to a function that accepts List<object>? I
cannot get the following easy-cheesy app to work. I get the following
error:

Argument '1': cannot convert from 'GenericsPOC.ArticleCollection' to
'System.Collections.Generic.IList<object>'

I don't know the details of what you can do with .NET generics, there's
a good reason why you *can't* do the above. Suppose your MapObject
method did:

list.Add(new FileStream());

You'd end up with a runtime exception, which is what generics tries to
sort out.

I suspect the way to fix the problem would be to make the MapObject
method generic (i.e. it takes a list of some type, and can only do
things with that type) - that would be the way to do it in Java
generics, anyway...
 

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

Top