Example #1
0
        /// <summary>Shortcut for testing whether the operand is equal to a non-null value</summary>
        /// <param name="code">The <see cref="CodeInstruction"/></param>
        /// <param name="value">The value</param>
        /// <returns>True if the operand has the same type and is equal to the value</returns>
        ///
        public static bool OperandIs(this CodeInstruction code, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (code.operand == null)
            {
                return(false);
            }
            var type        = value.GetType();
            var operandType = code.operand.GetType();

            if (AccessTools.IsInteger(type) && AccessTools.IsNumber(operandType))
            {
                return(Convert.ToInt64(code.operand) == Convert.ToInt64(value));
            }
            if (AccessTools.IsFloatingPoint(type) && AccessTools.IsNumber(operandType))
            {
                // ReSharper disable once CompareOfFloatsByEqualityOperator
                return(Convert.ToDouble(code.operand) == Convert.ToDouble(value));
            }
            return(Equals(code.operand, value));
        }