diff --git a/Projects/TerraTech/TerraTech/ObjectFieldMultiplier.cs b/Projects/TerraTech/TerraTech/ObjectFieldMultiplier.cs index acdc16f..bef6e22 100644 --- a/Projects/TerraTech/TerraTech/ObjectFieldMultiplier.cs +++ b/Projects/TerraTech/TerraTech/ObjectFieldMultiplier.cs @@ -23,6 +23,24 @@ namespace TerraTech { throw new ArgumentException( string.Format("Field {0} does not exist on {1}", fieldName, parentTraverse)); } + + // Verify TValue is a numeric type + if (!IsNumericType(typeof(TValue))) { + throw new ArgumentException(string.Format("Type {0} must be a numeric type", typeof(TValue).Name)); + } + } + + private static bool IsNumericType(Type type) { + return type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || type == typeof(ushort) || + type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong) || + type == typeof(float) || type == typeof(double) || type == typeof(decimal); + } + + private static TValue MultiplyValues(TValue a, TValue b) { + // Convert to double for the multiplication + double result = Convert.ToDouble(a) * Convert.ToDouble(b); + // Convert back to TValue + return (TValue)Convert.ChangeType(result, typeof(TValue)); } public TValue GetValue() { @@ -48,9 +66,7 @@ namespace TerraTech { public void Apply() { try { - dynamic originalDynamic = _originalValue; - dynamic multiplierDynamic = _multiplier.Value; - var newValue = (TValue)(originalDynamic * multiplierDynamic); + var newValue = MultiplyValues(_originalValue, _multiplier.Value); if (Main.debug.Value) Console.WriteLine("Applying to {0}: {1} * {2} = {3}", _fieldName, _originalValue, _multiplier.Value,