473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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;
}

}

Nov 17 '05 #1
4 25378
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.
"KC" <NO**@NONE.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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;
}

}

Nov 17 '05 #2
KC <NO**@NONE.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
You might want to look at this thread:

http://groups-beta.google.com/group/...9f8362a9f5ff52

Nov 17 '05 #4
KC
Thanks Bruce, this post answers the question perfectly ... Good read.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
You might want to look at this thread:

http://groups-beta.google.com/group/...9f8362a9f5ff52

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tomba | last post by:
hi, I'm trying to cast a LinkedList<EdgePoint> to Iterable<DiagramElement>. Just casting by (Iterable<DiagramElement>)list results in an error. However I think it should be possible as: -...
9
by: Kurt | last post by:
Hi I was trying to get this VB type code to work in C Sub SetColumns_Example( Dim ol As Outlook.Applicatio Dim MyFolder As MAPIFolde Dim itms As Item Dim itm As Objec Dim dtmStart As Date,...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
9
by: Roman Mashak | last post by:
Hello, All! I met this code recently on some open source sites. What may be the point of using such construction: typedef struct cmd { unsigned int cmdack; unsigned int code; unsigned int...
14
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
0
by: PJ6 | last post by:
Is there any way I can get the compiler to accept this kind of generic type upcasting at runtime? Paul Public Class GenericClass(Of SomeType As {New}) End Class Public Sub Test()
4
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
5
by: Microsoft Newsserver | last post by:
Hi, Im having a bit of difficulty working out how to do the following. method ( paramType? param1, DateTime? requiredByDate , paramType? param3) I have a row property which I need to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.