protected Lambda(NetworkMode mode, T action) { this.Mode = mode; this.action = action; Invoke = ExpressionsUtils.GenerateDelegateFromInvoker <T>(this, LocalInvoke); }
public static void RequiredArgumentEmail <T>(this T @object, Expression <Func <string> > propertyLambda) where T : class { var value = (string)ExpressionsUtils.GetValue(propertyLambda); if (new EmailAddressAttribute().IsValid(value) == false) { ThrowArgumentException(propertyLambda, $"Not valid email = {value}", context: @object); } }
public static void RequiredArgumentNotDefaultValue <T, TProperty>(this T @object, Expression <Func <TProperty> > propertyLambda) where T : class where TProperty : struct { var value = ExpressionsUtils.GetValue(propertyLambda); if (value.Equals(default(TProperty))) { var typeParameterType = typeof(TProperty); ThrowArgumentException(propertyLambda: propertyLambda, message: $"{typeParameterType.Name} equals default type value = {value}", context: @object); } }
public static void RequiredArgumentDateIsActual <T>(this T @object, Expression <Func <DateTime> > propertyLambda, int yearDelta = 1) where T : class { var dateNow = DateTime.Now; var fromDate = dateNow.AddYears(-yearDelta); var toDate = dateNow.AddYears(yearDelta); var value = (DateTime)ExpressionsUtils.GetValue(propertyLambda); if (value < fromDate || value > toDate) { ThrowArgumentException(propertyLambda, $"DateTime is out of range values from = {fromDate}, to = {toDate}", context: @object); } }
public static void RequiredNotNull <T, TProperty>(this T @object, Expression <Func <TProperty> > propertyLambda) where T : class { var value = ExpressionsUtils.GetValue(propertyLambda); if (value == null) { ThrowInvalidOperationException(propertyLambda: propertyLambda, context: @object); } var typeParameterType = typeof(TProperty); if (typeParameterType == typeof(string) && string.IsNullOrEmpty((string)value)) { ThrowInvalidOperationException(propertyLambda: propertyLambda, context: @object); } }
public static void RequiredArgumentEnumIsDefinedValue <T, TProperty>(this T @object, Expression <Func <TProperty> > propertyLambda) where T : class where TProperty : struct, IConvertible { if (typeof(TProperty).IsEnum == false) { throw new InvalidOperationException("TProperty must be an enumerated type"); } var value = ExpressionsUtils.GetValue(propertyLambda); var propertyType = typeof(TProperty); if (Enum.IsDefined(propertyType, value) == false) { ThrowArgumentException(propertyLambda, $"Not defined {propertyType.Name} value = {value}", context: @object); } }
public static void RequiredListNotEmpty <T, TProperty>(this T @object, Expression <Func <IEnumerable <TProperty> > > propertyLambda) where T : class { RequiredNotNull(@object, propertyLambda); var value = ExpressionsUtils.GetValue(propertyLambda) as IEnumerable <TProperty>; if (value == null) { throw new InvalidOperationException($"value must be IEnumerable<{typeof(TProperty).Name}>"); } if (value.Any() == false) { var typeParameterType = typeof(TProperty); ThrowInvalidOperationException(propertyLambda: propertyLambda, message: $"IEnumerable<{typeParameterType.Name}> must not be empty", context: @object); } }
private static ExceptionParameters GenerateExceptionParameters <T>(Expression <Func <T> > propertyLabmda, string message, object context) { var ret = new ExceptionParameters(); var messageParts = new List <string>(); ret.PropertyName = ExpressionsUtils.GetPropertyName <T>(propertyLabmda); messageParts.Add($"paramName: {ret.PropertyName}"); if (string.IsNullOrEmpty(message) == false) { messageParts.Add(message); } if (context != null) { messageParts.Add($"context: {JsonConvert.SerializeObject(context)}"); } ret.ExcMessage = string.Join(", ", messageParts); return(ret); }
public SafeEvent() { Invoke = ExpressionsUtils.GenerateDelegateFromInvoker <T>(this, InvokeAction); }
protected LambdaWrapper(T action) { this.action = action; Invoke = ExpressionsUtils.GenerateLambdaFromInvoker <T>(this, LocalInvoke); }
protected NetworkLambda(NetworkMode mode, T action) : base(mode, action) { NetworkHandler.Instance.Register(Receive, out Id, out Sender, out Disposer); agrumentTypes = ExpressionsUtils.ParameterTypesOfDelegate <T>().ToArray(); }