[Mono-dev] Casting bug with generic collections

Michi Henning michi at zeroc.com
Mon Sep 24 04:54:50 EDT 2007


I would have put this into bugzilla, but it wouldn't let me, so I'm 
reporting it here...

The code below compiles and runs fine with VS 2005 and prints "a".

With Mono 1.2.4, I get a cast exception:

Unhandled Exception: System.InvalidCastException: Cannot cast from 
source type to destination type.
  at System.Collections.Generic.Dictionary 
2+KeyColleciton[System.String,System.String].System.Collections.ICollection.CopyTo 
(System.Array , Int32 ) [0x00000]

Here is the code:

            System.Collections.Generic.Dictionary<string, string> d = 
new System.Collections.Generic.Dictionary<string, string>();
            d["a"] = "b";
            System.Collections.ICollection keys = d.Keys;
            object[] keyArray = new object[d.Count];
            keys.CopyTo(keyArray, 0);
            System.Console.WriteLine(keyArray[0]);

Cheers,

Michi.



More information about the Mono-devel-list mailing list