Bugzilla – Bug 322952
[GMCS] Can't convert IList<T> to T array
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by rodrigobamboo@gmail.com 2006-12-14 10:07:00 MST ---- using System.Collections.Generic; class CantCastGenericListToArray { public static void Main(string[] args) { IList<string> list = new string[] { "foo", "bar" }; string[] array = (string[])list; if (list.Count != array.Length) { throw new System.ApplicationException(); } } } Actual Results: CantCastGenericListToArray.cs(8,34): error CS0030: Cannot convert type `System.Collections.Generic.IList<string>' to `string[]' Compilation failed: 1 error(s), 0 warnings Expected Results: Successful compilation. ---- Additional Comments From miguel@ximian.com 2006-12-18 19:17:40 MST ---- Per a discussion with Martin, these are just a few cases missing in ExplicitReferenceConversionExists and ExplicitReferenceConversion in mcs/convert.cs The relevant piece of the spec that is missing is: 13.2.3 "Explicit Reference Conversions" is missing the last two conversions that only apply to the generics compiler. These are: "* From a one-dimensional array-type S[] to System.Collections.Generic.IList<T> and its base interfaces, provided there is an explicit reference conversion from S to T. * From System.Collections.Generic.IList<T> and its base interfaces to a one-dimensional" array-type S[], provided there is an implicit or explicit reference conversion from S[] to System.Collections.Generic.IList<T>. This is precisely when either S and T are the same type or there is an implicit or explicit reference conversion from S to T. ---- Additional Comments From miguel@ximian.com 2006-12-18 19:18:17 MST ---- The VM support is already in place, I ran the MS compiled programs on Mono without any problem. ---- Additional Comments From dmitchell@logos.com 2007-02-04 00:24:44 MST ---- Created an attachment This patch will fix https://bugzilla.novell.com/show_bug.cgi?id=MONO80260 ---- Additional Comments From dmitchell@logos.com 2007-02-04 00:29:18 MST ---- I've attached a patch that will fix the problem on the compiler end. In testing the patch, I discovered that the runtime does not always behave like the Microsoft implementation. For example, the following program will compile under the Microsoft implementation, but the conversion will throw an exception: using System.Collections.Generic; class CantCastGenericListToArray { public static void Main(string[] args) { IList<object> list = new object[] { "foo", "bar" }; string[] array = (string[])list; if (list.Count != array.Length) { throw new System.ApplicationException(); } } } However, no exceptions will be thrown in Mono. Does this qualify as a separate bug, or should this one be left open? ---- Additional Comments From miguel@ximian.com 2007-03-05 18:54:32 MST ---- Commited the patch, thanks! Yes, we do need a separate bug for the runtime piece, I have it filed as 81044. Miguel Imported an attachment (id=171098) Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>Linux dapper 2.6.15-26-k7</cf_op_sys_details> Unknown operating system unknown. Setting to default OS "Other".