public static void TryConvertMismatchedTypes(ref object expectedValue, ref object actualValue) { if (expectedValue == null || actualValue == null) { return; } Type expectedType = expectedValue.GetType(); Type actualType = actualValue.GetType(); if (expectedType != actualType) { // Try to convert the actual value to the expected type. if (!ValueWrapper.TryConvertToType(actualValue, expectedType, out actualValue)) { // Try to convert the expected value to the actual type. ValueWrapper.TryConvertToType(expectedValue, actualType, out expectedValue); } } }
public static bool TryConvertToType <TActual, TExpected>(TActual value, out TExpected convertedValue) { convertedValue = default(TExpected); if (value == null) { return(true); } // Try to convert the value to the expected type Type expectedType = typeof(TExpected); object tempConvertedValue; if (ValueWrapper.TryConvertToType(value, expectedType, out tempConvertedValue)) { convertedValue = (TExpected)tempConvertedValue; return(true); } return(false); }