简体   繁体   English

比较一个对象的两个实例

[英]Compare two instances of an object

I'm not really sure if what I'm looking for exists, but here it goes, 我不确定自己要寻找的东西是否存在,但是就这样,

    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

I basically want to be able to have the following code return true, 我基本上希望能够使以下代码返回true,

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

Is there some interface or something that I can implement? 是否有一些接口或可以实现的功能? Is doing == the same in this instance as doing .Equals or .CompareTo ? 在这种情况下==与.Equals.CompareTo是否相同?

Thanks in advanced! 提前致谢!

William 威廉

Update 更新

I want to make sure that I am implementing all of the overloads/operators correctly - so let me know what you think - 我想确保我正确地实现了所有的重载/运算符-所以让我知道你的想法-

        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;
        }

You can do this : on each method you test your value to return 您可以执行以下操作:在每种方法上测试返回的值

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;
        }

    }

You can see other sample here 您可以在这里看到其他示例

You can use Generics. 您可以使用泛型。 Take a look into tgis example: 看一下tgis示例:

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

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

And read here for some additional information. 在这里阅读一些其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM