using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.ComponentModel; using System.Globalization; using System.Diagnostics.CodeAnalysis; namespace MT.Platform.Common { /// /// This is a helper class for validation. /// public sealed class Validate { private Validate() { } /// /// Determines whether the specified is null. /// In case of null the specified exception will be thrown. /// /// The o. /// The formated message. /// The args. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "o"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "formated"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter"), SuppressMessage("Microsoft.DesignRules", "CA1004", Justification = "For a better readability.")] public static void IsNotNull(object o, string formatedMessage, params object[] args) where TExceptionType : System.Exception, new() { IsFalse(o == null, formatedMessage, args); } /// /// Determines whether the specified is true. /// In case of the condition is false the specified condition is thrown. /// /// if set to true [condition]. /// The formated message. /// The args. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "formated"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter"), SuppressMessage("Microsoft.DesignRules", "CA1004", Justification = "For a better readability.")] public static void IsTrue(bool condition, string formatedMessage, params object[] args) where TExceptionType : System.Exception, new() { IsFalse < TExceptionType >(!condition, formatedMessage, args); } /// /// Determines whether the specified is false. /// In case of the condition is true the specified exception is thrown. /// /// if set to true [condition]. /// The formated message. /// The args. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter"), SuppressMessage("Microsoft.DesignRules", "CA1004", Justification = "For a better readability.")] public static void IsFalse(bool condition, string formatedMessage, params object[] args) where TExceptionType : System.Exception, new() { if (condition) { TExceptionType exception = new TExceptionType(); SetProperty(exception, "_message", string.Format(CultureInfo.CurrentCulture, formatedMessage, args), false); throw exception; } } /// /// Sets a field / property of an object. /// /// The object of which the field / property should be set /// The name of the field / property /// The value of the property /// True if it shall operate case insensitive static private void SetProperty(object obj, string propertyName, string propertyValue, bool ignoreCase) { BindingFlags bindings = BindingFlags.Public | BindingFlags.Instance; if (ignoreCase) { bindings |= BindingFlags.IgnoreCase; } Type type = obj.GetType(); // look for a setable property PropertyInfo property = type.GetProperty(propertyName, bindings); if (property != null && property.CanWrite) { property.SetValue(obj, propertyValue, null); return; } // look for a public field FieldInfo field = type.GetField(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (field != null) { field.SetValue(obj, propertyValue); return; } } } }