How to deeply compare non-serializable objects?

Let's say we have two cursor files that are loaded into C # objects:

using (var ms = new MemoryStream(Resource.file1))
    _cursor1 = new System.Windows.Input.Cursor(ms);

using (var ms = new MemoryStream(Resource.file2))
    _cursor2 = new System.Windows.Input.Cursor(ms);

      

There are reasons why I would like to compare these objects (for example, suppose what file1

could be a copy file2

and I would like to discover it). I have a method that tries to deserialize objects into byte arrays in order to finally compare arrays like this:

public static byte[] ToByteArray(this object obj)
{
    var bf = new BinaryFormatter();
    using (var ms = new MemoryStream())
    {
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }
}

      

Unfortunately, usage throws a serialization error:

var equal = _cursor1.ToByteArray().SequenceEqual(_cursor2.ToByteArray());

      

Additional Information: Enter "System.Windows.Input.Cursor" in Assembly 'PresentationCore, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35' is not marked serializable.

What is the way to compare such objects?

+3


source to share


1 answer


It is much easier to compare memory streams to see if they are equal or not.

Even more, you can choose what you want to compare and what not. If you try to compare Cursor objects, it is possible that they have internal data that makes them different, even if they have the same shape.

I think you know how to compare MemoryStreams

, otherwise just say it.



EDIT : Ok. As far as I can see that is your only option. After decompiling the cursor class, almost all of the work is done in unmanaged code, so you won't have access to it.

  [DllImport("user32.dll", BestFitMapping=false, CharSet=CharSet.Auto, EntryPoint="LoadImage", ExactSpelling=false, SetLastError=true, ThrowOnUnmappableChar=true)]
        internal static extern NativeMethods.CursorHandle LoadImageCursor(IntPtr hinst, string stName, int nType, int cxDesired, int cyDesired, int nFlags);

      

+1


source







All Articles