private void AssociatedObjectPreviewInvoke(object sender, PreviewInvokeEventArgs e) { try { var comparerType = typeof(Comparer <>).MakeGenericType(Lhs.GetType()); var defaultProperty = comparerType.GetTypeInfo().GetDeclaredProperty("Default"); var comparer = (IComparer)defaultProperty.GetValue(null); var comparerResult = comparer.Compare(Lhs, System.Convert.ChangeType(Rhs, comparerType)); switch (CompareOperator) { case Op.Eq: e.Cancelling = !(comparerResult == 0); break; case Op.Gt: e.Cancelling = !(comparerResult > 0); break; case Op.GtEq: e.Cancelling = !(comparerResult >= 0); break; case Op.Lt: e.Cancelling = !(comparerResult < 0); break; case Op.LtEq: e.Cancelling = !(comparerResult <= 0); break; } } catch (Exception ex) { Debug.WriteLine(ex); e.Cancelling = true; } }