示例#1
0
        public bool SetValue(Type type, object value, uint address, bool absoluteAddress = false, uint?mask = null, int?shift = null)
        {
            if (value is string)
            {
                if (type == typeof(byte))
                {
                    value = ParsingUtilities.ParseByteNullable(value);
                }
                if (type == typeof(sbyte))
                {
                    value = ParsingUtilities.ParseSByteNullable(value);
                }
                if (type == typeof(short))
                {
                    value = ParsingUtilities.ParseShortNullable(value);
                }
                if (type == typeof(ushort))
                {
                    value = ParsingUtilities.ParseUShortNullable(value);
                }
                if (type == typeof(int))
                {
                    value = ParsingUtilities.ParseIntNullable(value);
                }
                if (type == typeof(uint))
                {
                    value = ParsingUtilities.ParseUIntNullable(value);
                }
                if (type == typeof(float))
                {
                    value = ParsingUtilities.ParseFloatNullable(value);
                }
                if (type == typeof(double))
                {
                    value = ParsingUtilities.ParseDoubleNullable(value);
                }
            }

            if (value == null)
            {
                return(false);
            }

            if (type == typeof(byte))
            {
                return(SetValue((byte)value, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(sbyte))
            {
                return(SetValue((sbyte)value, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(short))
            {
                return(SetValue((short)value, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(ushort))
            {
                return(SetValue((ushort)value, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(int))
            {
                return(SetValue((int)value, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(uint))
            {
                return(SetValue((uint)value, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(float))
            {
                return(SetValue((float)value, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(double))
            {
                return(SetValue((double)value, address, absoluteAddress, mask, shift));
            }

            throw new ArgumentOutOfRangeException("Cannot call ProcessStream.SetValue with type " + type);
        }
示例#2
0
        public bool SetValueRoundingWrapping(
            Type type, object value, uint address, bool absoluteAddress = false, uint?mask = null, int?shift = null)
        {
            // Allow short circuiting if object is already of type
            if (type == typeof(byte) && value is byte byteValue)
            {
                return(SetValue(byteValue, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(sbyte) && value is sbyte sbyteValue)
            {
                return(SetValue(sbyteValue, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(short) && value is short shortValue)
            {
                return(SetValue(shortValue, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(ushort) && value is ushort ushortValue)
            {
                return(SetValue(ushortValue, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(int) && value is int intValue)
            {
                return(SetValue(intValue, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(uint) && value is uint uintValue)
            {
                return(SetValue(uintValue, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(float) && value is float floatValue)
            {
                return(SetValue(floatValue, address, absoluteAddress, mask, shift));
            }
            if (type == typeof(double) && value is double doubleValue)
            {
                return(SetValue(doubleValue, address, absoluteAddress, mask, shift));
            }

            value = ParsingUtilities.ParseDoubleNullable(value);
            if (value == null)
            {
                return(false);
            }

            if (type == typeof(byte))
            {
                value = ParsingUtilities.ParseByteRoundingWrapping(value);
            }
            if (type == typeof(sbyte))
            {
                value = ParsingUtilities.ParseSByteRoundingWrapping(value);
            }
            if (type == typeof(short))
            {
                value = ParsingUtilities.ParseShortRoundingWrapping(value);
            }
            if (type == typeof(ushort))
            {
                value = ParsingUtilities.ParseUShortRoundingWrapping(value);
            }
            if (type == typeof(int))
            {
                value = ParsingUtilities.ParseIntRoundingWrapping(value);
            }
            if (type == typeof(uint))
            {
                value = ParsingUtilities.ParseUIntRoundingWrapping(value);
            }

            return(SetValue(type, value.ToString(), address, absoluteAddress, mask, shift));
        }