Bug 14026 - System.InvalidCastException at (wrapper unknown) object:gsharedvt_in ()
Summary: System.InvalidCastException at (wrapper unknown) object:gsharedvt_in ()
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.4.0
Hardware: Other Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2013-08-16 06:01 UTC by artur.drobinskiy
Modified: 2013-08-20 10:11 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description artur.drobinskiy 2013-08-16 06:01:20 UTC
Initially posted in forums (http://forums.xamarin.com/discussion/7103/system-invalidcastexception-at-wrapper-unknown-object-gsharedvt-in?new=1), it was suggested to file a bug here.


With the latest beta (Xamarin.iOS 6.4.3) I got the exception when deserializing xml received from web service
(especially, when deserializing Dictionary<Enum, int>)
:

System.AggregateException: ---> System.InvalidCastException: Cannot cast from source type to destination type. at (wrapper managed-to-native) object:__icall_wrapper_mono_object_castclass_unbox (object,intptr) at System.Collections.Generic.Dictionary2[CompanyName.Enumerations.Pub.ImageSize,System.Int32].ToTKey (System.Object key) [0x00000] in <filename unknown>:0 
  at (wrapper unknown) object:gsharedvt_in ()
  at (wrapper unknown) object:gsharedvt_out ()
  at System.Collections.Generic.Dictionary
2[CompanyName.Enumerations.Pub.ImageSize,System.Int32].System.Collections.IDictionary.Add (System.Object key, System.Object value) [0x00000] in :0 at System.Runtime.Serialization.DictionaryTypeMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id) [0x00000] in :0 at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x00000] in :0 at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00000] in :0 at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id, Boolean empty) [0x00000] in :0


The code worked with Xamarin.Ios Stable (6.4.1)

Here's the failing test:
        [TestMethod]
        public void ImagesSerialization()
        {
            var instance = new ImagesSerializationTestDTO();
            instance.Images = new Dictionary<EnumTest, int>();
            instance.Images.Add(EnumTest.One, 10);


            MemoryStream stream = new MemoryStream();
            DataContractSerializer serializer = new DataContractSerializer(typeof(ImagesSerializationTestDTO));
            serializer.WriteObject(stream, instance);

            stream.Position = 0;
            string serializedText = new StreamReader(stream).ReadToEnd();
            stream.Position = 0;
            ImagesSerializationTestDTO deserialized = serializer.ReadObject(stream) as ImagesSerializationTestDTO;

            Assert.AreEqual(1, deserialized.Images.Count);

        }


[DataContract]
    public enum EnumTest : int
    {
        [EnumMember]
        One = 1,
        [EnumMember]
        Two = 2,
    }
    [DataContract(Namespace = "ServiceLibraryTests")]
    [KnownType(typeof(EnumTest))]
    public class ImagesSerializationTestDTO
    {
        [DataMember]
        public Dictionary<EnumTest, int> Images
        {
            get;
            set;
        }
    }
Comment 1 Zoltan Varga 2013-08-20 10:11:41 UTC
Fixed in master/mono-3-2. Backported to monotouch-6.4-series.