Bug? Forms4.2 on VS2019 can't serialize a class that VS2017 on 4.0 can

ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

Moving an existing Xamarin solution from Android 5 to Android 9.
Moving from Visual Studio 2017 to VS2019.
But no logic changes: Just tweeks to make the app run.

I have a class of ScanDocument with properties for a document that has been scanned (via USB scanner on Android tablet).
This class has zero changes between the 2017 and 2019 solutions.
Yet - 2017 has no issue serializing the class where 2019 throws “There was a problem reflecting type bla blah blah”

        public void Save(string filePath)
        {
            bool result = false;
            filePath = System.IO.Path.ChangeExtension(filePath, ".sdoc");

            try
            {
                fileHelper.DeleteFile(filePath);

                var xmlSerializer = new XmlSerializer(typeof(ScanDocument)); // <--- Here
                var stream = FHelper.Instance().CreateTextStreamWriter(filePath, false);
                if (stream != null)
                {
                    xmlSerializer.Serialize(stream, this);
                    stream.Flush(); //Because I'm paranoid
                    stream.Close();
                    stream.Dispose();
                }

                result = true;
                SavePath = filePath;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                result = false;
            }
        }

So what changed in the XMLSerializer, if it wasn’t a change in the class being serialized?

So I tried testing with a tiny little dummy class of only one property. Figured I'd start adding types until I found the offender. It turns out that as soon as the class inherits from BindableObject then reflection breaks, meaning you can't serialize it.

public class DummyDoc : BindableObject , IEqualityComparer<ScanDocument> - Fails

public class DummyDoc : BindableObject // , IEqualityComparer<ScanDocument> - Fails

public class DummyDoc// : BindableObject , IEqualityComparer<ScanDocument> - Success

Anyone have a guess if this is a problem is Xamarin Forms... Visual Studio 2019... something else? Its all Microsoft any more so I lodge an issue.
https://developercommunity.visualstudio.com/content/problem/807939/xmlserializer-fails-on-2019-succeeds-in-2017-for-s.html

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Just for completeness, I would try serializing public class DummyDoc : IEqualityComparer<ScanDocument> but if the offender is BindableObject that would obviously indicate an issue with changes made between Xamarin.Forms 4.0 and 4.2.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @JoeManke said:
    Just for completeness, I would try serializing public class DummyDoc : IEqualityComparer<ScanDocument>

    I'll go ahead and do that in the morning. But since implimenting the interface is all methods and no properties, I would be surprised to find it affected anything.

    but if the offender is BindableObject that would obviously indicate an issue with changes made between Xamarin.Forms 4.0 and 4.2.

    Yeah. Very much an "oh sh!t" moment since it stops our app dead in its tracks.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    cc @JoeManke

    I updated the Forms nugget this morning. Not a big jump. From 4.3.0.908675 to 4.3.0.947036

    Tried with just IEqualityComparer - that worked
    Tried again with BindableObject - that failed (just to confirm)

    So I guess I need to lodge this in the Xamarin Nugget Issues page.
    https://github.com/xamarin/Xamarin.Forms/issues/8400

    Class to be serialized**

        public class DummyDoc :BindableObject //IEqualityComparer<ScanDocument>// , IEqualityComparer<ScanDocument>
        {
            public DummyDoc()
            {
    
            }
    
            public string SavePath { get; set; }
    
    
            public override string ToString()
            {
                return "Dummy";
            }
    
            public bool Equals(ScanDocument x, ScanDocument y)
            {
    
                return true;
            }
    
            public bool Equals(ScanDocument y)
            {
                return Equals(this, y);
            }
    
            /// <summary>NotImplementedException
            /// 
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public int GetHashCode(ScanDocument obj)
            {
                throw new NotImplementedException();
            }
        }
    

Sign In or Register to comment.