[Mono-bugs] [Bug 80299][Wis] New - Array.CopyTo/Array.Copy doesn't support Enum to underlying-type conversion

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Mon Dec 18 06:53:36 EST 2006

Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by andyhume32 at yahoo.co.uk.


--- shadow/80299	2006-12-18 06:53:36.000000000 -0500
+++ shadow/80299.tmp.6698	2006-12-18 06:53:36.000000000 -0500
@@ -0,0 +1,73 @@
+Bug#: 80299
+Product: Mono: Class Libraries
+Version: 1.2
+OS Details: 
+Status: NEW   
+Priority: Wishlist
+Component: CORLIB
+AssignedTo: mono-bugs at ximian.com                            
+ReportedBy: andyhume32 at yahoo.co.uk               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+Summary: Array.CopyTo/Array.Copy doesn't support Enum to underlying-type conversion
+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-
+  at System.Array.CopyTo (System.Array array, Int32 index) [0x00089] in 
+  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 [...]"
+"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 
+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...

More information about the mono-bugs mailing list