Bugzilla – Bug 322991
[PATCH] Array.CopyTo/Array.Copy doesn't support Enum to underlying-type conversion
Last modified: 2007-09-15 21:24:46 UTC
---- Reported by andyhume32@yahoo.co.uk 2006-12-18 06:53:36 MST ---- In code developed on the MSFT CLR I have: enum ObexHeaderId : byte { .... } and then ObexHeaderId[] srcEnums = ...; byte[] dstBytes = new byte[srcEnums.Length]; srcEnums.CopyTo(dstBytes, 0); That works on MSFT CLR (desktop v2), but fails on Mono at the Array.CopyTo with: System.ArrayTypeMismatchException : (Types: source=Brecham.Obex.ObexHeaderId; target=System.Byte) at System.Array.Copy (System.Array sourceArray, Int32 sourceIndex, System.Array destinationArray, Int32 destinationIndex, Int32 length) [0x00174] in C:\cygwin\tmp\scratch\mono-1.2.2.1 \mcs\class\corlib\System\Array.cs:932 at System.Array.CopyTo (System.Array array, Int32 index) [0x00089] in C:\cygwin\tmp\scratch\mono-1.2.2.1\mcs\class\corlib\System\Array.cs:1752 at Brecham.Obex.ObexClientSession.LogHeaderIds (System.String message, Brecham.Obex.ObexHeaderId[] keys) [0x00000] [...] It fails on the NETCFv2 with InvalidCastException, btw. A workaround is straightforward. I've changed my code to use the elements manually, but a for loop over the array casting the element on copying, is of course straighforward. ECMA's Array.pdf in tr-084.zip says: "If sourceArray and destinationArray are of different types, System.Array.Copy performs widening conversions on the elements [...]" and "If the necessary conversion is a narrowing conversion, a System.ArrayTypeMismatchException exception is thrown. [Note: For information regarding valid conversions performed by this method, see System.Convert.]" There is hardly any text in the CopyTo method, so do we assume that text applies to it too? I presume the necessary conversion here is not a "narrowing conversion", and nor is it a "widening conversion". So where does this case lie? Again MSDN seems not to cover this case. A set of test cases is included. They show that copy only is successful when the type of the destination array is the same as the 'underlying' type of the enum. For instance, counterintuitively, an array of 'byte' enums fails to copy to an Int32 array. This maybe suggests that this behaviour on the MSFT CLR is just by side effect and not intentional... ---- Additional Comments From andyhume32@yahoo.co.uk 2006-12-18 06:53:59 MST ---- Created an attachment (id=171125) C# code file containing NUnit test cases ---- Additional Comments From robertj@gmx.net 2006-12-19 15:26:38 MST ---- ves_icall_System_Array_SetValueImpl () doesn't take enums into account. -> runtime ---- Additional Comments From robertj@gmx.net 2006-12-19 15:27:07 MST ---- Created an attachment (id=171126) icall.diff ---- Additional Comments From lupus@ximian.com 2006-12-20 07:25:55 MST ---- Robert, please commit the patch and the tests. Thanks! ---- Additional Comments From robertj@gmx.net 2006-12-21 20:24:12 MST ---- Fixed in SVN r69910:69911. Imported an attachment (id=171125) Imported an attachment (id=171126) Unknown operating system unknown. Setting to default OS "Other".