I'm writing a general alerts system based on user-generated conditions. Test values & actual values are handled as objects since they could be numeric, boolean, or string depending on the selected property. It needs to be error tolerant and fail gracefully.
So I'm looking for a general purpose function in C# to make comparisons (greater/less/equal) for various data types.
Using the IComparable interface doesn't work as it is type-specific, for example this will crash if testValue is Int32 and comparsonValue is decimal:
object testValue = (int)10;
object comparisonValue = (decimal)11;
var comparison = ((IComparable)testValue).CompareTo(comparisonValue)
// Crashes with {System.SystemException}: {"Object must be of type Int32."}
Is there any other general purpose comparison function in C#? I hope I don't have to code everything up myself.