public string Invocation() { var invocation = $"new {BuiltType.FullNameInCode()}({Parameters.Select(x => x.Usage).Join(", ")})"; if (Setters.Any()) { invocation += $"{{{Setters.Select(x => x.Assignment()).Join(", ")}}}"; } return(invocation); }
public void WriteExpressions(LambdaDefinition definition) { // No next, not disposable var isDisposed = BuiltType.CanBeCastTo <IDisposable>() || BuiltType.CanBeCastTo <IAsyncDisposable>(); var callCtor = Expression.New(Ctor, Parameters.Select(definition.ExpressionFor)); if (Next == null && !isDisposed && !Setters.Any()) { definition.Body.Add(callCtor); } else { var variableExpr = Expression.Parameter(BuiltType, Variable.Usage); definition.RegisterExpression(Variable, variableExpr); definition.Assign(variableExpr, callCtor); foreach (var setter in Setters) { var setMethod = BuiltType.GetProperty(setter.PropertyName).SetMethod; var value = definition.ExpressionFor(setter.Variable); var call = Expression.Call(variableExpr, setMethod, value); definition.Body.Add(call); } if (isDisposed) { definition.RegisterDisposable(variableExpr, Variable.VariableType); } if (Next == null) { definition.Body.Add(definition.ExpressionFor(Variable)); } else { if (Next is IResolverFrame next) { next.WriteExpressions(definition); } else { throw new InvalidCastException($"{Next.GetType().GetFullName()} does not implement {nameof(IResolverFrame)}"); } } } }
/// <summary> /// Applies the result of the validation, valid if result is true, invalid otherwise /// </summary> /// <param name="result">Flag indicating the state of the RuleSet</param> /// <param name="sourceElement">If this action has no target target this /// instead (The source from the rule)</param> /// Element created at 07/11/2014,6:17 AM by Charles internal void ApplyResult(bool result, BindableObject sourceElement = null) { try { var target = sourceElement ?? Element; var targetType = target.GetType(); //Process the property defined inline (if any) if (!string.IsNullOrEmpty(Property)) { var pi = GetPropertyInfo(Property, targetType); if (pi != null) { AppplyValueToProperty(target, pi, result ? ValidValue : InvalidValue); } } //Process the setters collection if present if (Setters == null || !Setters.Any()) { return; } foreach (var s in Setters) { var propinfo = GetPropertyInfo(s.Property, targetType); if (propinfo != null) { AppplyValueToProperty(target, propinfo, result ? s.ValidValue : s.InvalidValue); } } } catch (Exception ex) { throw new InvalidCastException( string.Format("Could not convert {0} to {1}", result ? ValidValue : InvalidValue, PropertyInfo.PropertyType.Name), ex); } }
public bool hasSetterFor(Type type) { return(Setters.Any(attr => attr.type == type)); }