Comparing 2 custom objects - C#

前端 未结 6 1475
鱼传尺愫
鱼传尺愫 2021-01-05 11:48

I need to write a generic method in the base class that would accept 2 objects as parameters and compares them for equality.

Ex:

public abstract cla         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 12:19

    public void CompareTwoObjects()
    {
        try {   
            byte[] btArray = ObjectToByteArray(object1);   //object1 is you custom object1
            byte[] btArray2 = ObjectToByteArray(object2); //object2 is you custom object2
            bool result = ByteArrayCompare(btArray, btArray2);
        } catch (Exception ex) {
            throw ex;
        }
    }
    
    public byte[] ObjectToByteArray(object _Object)
    {
        try {
            // create new memory stream
            System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();
    
            // create new BinaryFormatter
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
                = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    
            // Serializes an object, or graph of connected objects, to the given stream.
            _BinaryFormatter.Serialize(_MemoryStream, _Object);
    
            // convert stream to byte array and return
            return _MemoryStream.ToArray();
        } catch (Exception _Exception) {
            // Error
            Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
        }
    
        // Error occured, return null
        return null;
    }
    
    public bool ByteArrayCompare(byte[] a1, byte[] a2)
    {
        if (a1.Length != a2.Length)
            return false;
    
        for (int i = 0; i < a1.Length; i++)
            if (a1[i] != a2[i])
                return false;
    
        return true;
    }
    

提交回复
热议问题