Пример #1
0
        /** <inheritdoc /> */
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }

            BinaryObject that = obj as BinaryObject;

            if (that != null)
            {
                if (_data == that._data && _offset == that._offset)
                {
                    return(true);
                }

                // 1. Check headers
                if (_header == that._header)
                {
                    // 2. Check if objects have the same field sets.
                    InitializeFields();
                    that.InitializeFields();

                    if (_fields.Keys.Count != that._fields.Keys.Count)
                    {
                        return(false);
                    }

                    foreach (int id in _fields.Keys)
                    {
                        if (!that._fields.ContainsKey(id))
                        {
                            return(false);
                        }
                    }

                    // 3. Check if objects have the same field values.
                    foreach (KeyValuePair <int, int> field in _fields)
                    {
                        object fieldVal     = GetField <object>(field.Value, null);
                        object thatFieldVal = that.GetField <object>(that._fields[field.Key], null);

                        if (!Equals(fieldVal, thatFieldVal))
                        {
                            return(false);
                        }
                    }

                    // 4. Check if objects have the same raw data.
                    // ReSharper disable ImpureMethodCallOnReadonlyValueField (method is not impure)
                    using (var stream = new BinaryHeapStream(_data))
                        using (var thatStream = new BinaryHeapStream(that._data))
                        {
                            var rawOffset     = _header.GetRawOffset(stream, _offset);
                            var thatRawOffset = that._header.GetRawOffset(thatStream, that._offset);

                            return(BinaryUtils.CompareArrays(_data, _offset + rawOffset, _header.Length - rawOffset,
                                                             that._data, that._offset + thatRawOffset, that._header.Length - thatRawOffset));
                        }
                    // ReSharper restore ImpureMethodCallOnReadonlyValueField
                }
            }

            return(false);
        }