比较对象的两个实例

时间:2012-04-25 13:34:03

标签: c# object comparison

我不确定我所寻找的是否存在,但在这里,

    public class OptionType : IComp
    {
        public static readonly FIXTag Tag = 201;

        public enum Types
        {
            Put = 'P',
            Call = 'C'
        }

        private abstract class CharValues
        {
            public const char Put  = '0';
            public const char Call = '1';
        }

        public abstract class ByteValues
        {
            public const byte Put = (byte)CharValues.Put;
            public const byte Call = (byte)CharValues.Call;
        }

        #region Instances
        public readonly Types Type;
        public readonly DataOptionType DataType;
        public readonly OptionType InverseType;
        public readonly char CharValue;
        public readonly byte ByteValue;

        private OptionType(Types Type, DataOptionType DataType, FIX.OptionType InverseType, char CharValue, byte ByteValue)
        {
            this.Type        = Type;
            this.DataType    = DataType;
            this.InverseType = InverseType;
            this.CharValue   = CharValue;
            this.ByteValue   = ByteValue;

            if (InverseType == null)
            {
                this.InverseType = (Type == Types.Call)
                    ? new OptionType(Types.Put, DataOptionType.Put, this, CharValues.Put, ByteValues.Put)
                    : new OptionType(Types.Call, DataOptionType.Call, this, CharValues.Call, ByteValues.Call);
            }
        }

        public static OptionType Put = new OptionType(Types.Put, DataOptionType.Put, FIX.OptionType.Call, CharValues.Put, ByteValues.Put);
        public static OptionType Call = new OptionType(Types.Call, DataOptionType.Call, FIX.OptionType.Put, CharValues.Call, ByteValues.Call);
        #endregion

我基本上希望能够让以下代码返回true,

    if (FIX.OptionType.Call == FIX.OptionType.Put.InverseType)
    {
        // Do Something
    }

是否有一些界面或者我可以实现的东西?在这个例子中做==与做.Equals.CompareTo

相同

先谢谢!

威廉

更新

我想确保我正确实现所有重载/运算符 - 所以让我知道你的想法 -

        public static bool operator== (FIX.OptionType ValueA, FIX.OptionType ValueB)
        {
            return (ValueA.Type != ValueB.Type) ? true : false;
        }

        public static bool operator!= (FIX.OptionType ValueA, FIX.OptionType ValueB)
        {
            return (ValueA.Type != ValueB.Type) ? true : false;
        }

        public override bool Equals(object obj)
        {
            return (obj != null && obj is FIX.OptionType && (obj as FIX.OptionType).Type == Type)
                ? true
                : false;
        }

        public override int GetHashCode()
        {
            return (int)Type;
        }

2 个答案:

答案 0 :(得分:1)

您可以这样做:在每种方法上测试您的值以返回

public class myClass
    {
        public static bool operator== (myClass a, myClass b)
        {
            bool _bResult = false;

            // your code..

            return _bResult;
        }

        public static bool operator !=(myClass a, myClass b)
        {
            bool _bResult = false;

            // your code..

            return _bResult;
        }

    }

您可以看到其他示例here

答案 1 :(得分:0)

您可以使用泛型。 看看tgis示例:

public bool IsDataChanged<T>()
{           
    T value1 = GetValue2;
    T value2 = GetValue1();

    return !EqualityComparer<T>.Default.Equals(valueInDB, valueFromView);
}

请阅读here以获取更多信息。