public T Evaluate() => _variableProvider.GetValue(_variableName);
Esempio n. 2
0
        public IBoundVariable Bind(XmlLayoutElement element, T instance, IVariableProvider provider)
        {
            var value = provider.GetValue <P>(VariableName);

            return(new BoundVariable(element, instance, value, Setter));
        }
Esempio n. 3
0
 private Delegate BuildExpr(Expr e)
 {
     if (e.OnLiteral)
     {
         if (e.Literal.OnBooleanValue)
         {
             var v = e.Literal.BooleanValue;
             return((Func <T, Boolean>)(t => v));
         }
         else if (e.Literal.OnIntValue)
         {
             var v = e.Literal.IntValue;
             return((Func <T, int>)(t => v));
         }
         else if (e.Literal.OnRealValue)
         {
             var v = e.Literal.RealValue;
             return((Func <T, double>)(t => v));
         }
         else
         {
             throw new InvalidOperationException();
         }
     }
     else if (e.OnVariable)
     {
         var Name = e.Variable.Name;
         var f    = VariableProvider.GetValue(Name, null, null);
         if (f.Count == 0)
         {
             throw new InvalidOperationException(String.Format("VariableNotExist: {0}", Name));
         }
         else if (f.Count > 1)
         {
             throw new InvalidOperationException(String.Format("MultipleVariableExist: {0}", Name));
         }
         return(f.Single());
     }
     else if (e.OnFunction)
     {
         var Name           = e.Function.Name;
         var ParameterFuncs = e.Function.Parameters.Select(p => BuildExpr(p)).ToList();
         var f = VariableProvider.GetValue(Name, ParameterFuncs.Select(pf => GetReturnType(pf)).ToList(), ParameterFuncs);
         if (f.Count == 0)
         {
             throw new InvalidOperationException(String.Format("FunctionNotExist: {0}", Name));
         }
         else if (f.Count > 1)
         {
             throw new InvalidOperationException(String.Format("MultipleFunctionExist: {0}", Name));
         }
         return(f.Single());
     }
     else if (e.OnIf)
     {
         var l = BuildExpr(e.If.TruePart);
         var r = BuildExpr(e.If.FalsePart);
         if (l.GetType() != r.GetType())
         {
             throw new InvalidOperationException();
         }
         var Condition = (Func <T, Boolean>)(BuildExpr(e.If.Condition));
         var type      = l.GetType();
         if (type == typeof(Func <T, Boolean>))
         {
             var Left  = (Func <T, Boolean>)(l);
             var Right = (Func <T, Boolean>)(r);
             return((Func <T, Boolean>)(t => Condition(t) ? Left(t) : Right(t)));
         }
         if (type == typeof(Func <T, int>))
         {
             var Left  = (Func <T, int>)(l);
             var Right = (Func <T, int>)(r);
             return((Func <T, int>)(t => Condition(t) ? Left(t) : Right(t)));
         }
         if (type == typeof(Func <T, double>))
         {
             var Left  = (Func <T, double>)(l);
             var Right = (Func <T, double>)(r);
             return((Func <T, double>)(t => Condition(t) ? Left(t) : Right(t)));
         }
         throw new InvalidOperationException();
     }
     else if (e.OnAndAlso)
     {
         var Left  = (Func <T, Boolean>)(BuildExpr(e.AndAlso.Left));
         var Right = (Func <T, Boolean>)(BuildExpr(e.AndAlso.Right));
         return((Func <T, Boolean>)(t => Left(t) && Right(t)));
     }
     else if (e.OnOrElse)
     {
         var Left  = (Func <T, Boolean>)(BuildExpr(e.OrElse.Left));
         var Right = (Func <T, Boolean>)(BuildExpr(e.OrElse.Right));
         return((Func <T, Boolean>)(t => Left(t) || Right(t)));
     }
     else
     {
         throw new InvalidOperationException();
     }
 }