本文整理汇总了C#中Variables.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Variables.Add方法的具体用法?C# Variables.Add怎么用?C# Variables.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Variables
的用法示例。
在下文中一共展示了Variables.Add方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EvalExpression
protected override object EvalExpression(ParseTree tree, params object[] paramlist)
{
// if only left hand side available, this is not an assignment, simple evaluate expression
if (nodes.Count == 1)
return this.GetValue(tree, TokenType.AssignmentExpression, 0); // return the result
if (nodes.Count != 3)
{
tree.Errors.Add(new ParseError("Illegal EvalExpression format", 1092, this));
return null;
}
// ok, this is an assignment so declare the function or variable
// assignment only allowed to function or to a variable
ParseNode v = GetFunctionOrVariable(nodes[0]);
if (v == null)
{
tree.Errors.Add(new ParseError("Can only assign to function or variable", 1020, this));
return null;
}
ParseTreeEvaluator root = tree as ParseTreeEvaluator;
if (root == null)
{
tree.Errors.Add(new ParseError("Invalid parser used", 1040, this));
return null;
}
if (root.Context == null)
{
tree.Errors.Add(new ParseError("No context defined", 1041, this));
return null;
}
if (v.Token.Type == TokenType.VARIABLE)
{
// simply overwrite any previous defnition
string key = v.Token.Text;
root.Context.Globals[key] = this.GetValue(tree, TokenType.AssignmentExpression, 1);
return null;
}
else if (v.Token.Type == TokenType.Function)
{
string key = v.Nodes[0].Token.Text;
// function lookup is case insensitive
if (root.Context.Functions.ContainsKey(key.ToLower()))
if (!(root.Context.Functions[key.ToLower()] is DynamicFunction))
{
tree.Errors.Add(new ParseError("Built in functions cannot be overwritten", 1050, this));
return null;
}
// lets determine the input variables.
// functions must be of te form f(x;y;z) = x+y*z;
// check the function parameters to be of type Variable, error otherwise
Variables vars = new Variables();
ParseNode paramsNode = v.Nodes[2];
if (paramsNode.Token.Type == TokenType.Params)
{ // function has parameters, so check if they are all variable declarations
for (int i = 0; i < paramsNode.Nodes.Count; i += 2)
{
ParseNode varNode = GetFunctionOrVariable(paramsNode.Nodes[i]);
if (varNode == null || varNode.Token.Type != TokenType.VARIABLE)
{
tree.Errors.Add(new ParseError("Function declaration may only contain variables", 1051, this));
return null;
}
// simply declare the variable, no need to evaluate the value of it at this point.
// evaluation will be done when the function is executed
// note, variables are Case Sensitive (!)
vars.Add(varNode.Token.Text, null);
}
}
// we have all the info we need to know to declare the dynamicly defined function
// pass on nodes[2] which is the Right Hand Side (RHS) of the assignment
// nodes[2] will be evaluated at runtime when the function is executed.
DynamicFunction dynf = new DynamicFunction(key, nodes[2], vars, vars.Count, vars.Count);
root.Context.Functions[key.ToLower()] = dynf;
return null;
}
// in an assignment, dont return any result (basically void)
return null;
}
示例2: GetVariableValues
public Variables GetVariableValues(Document document, ISchema schema, VariableDefinitions variableDefinitions, Inputs inputs)
{
var variables = new Variables();
variableDefinitions.Apply(v =>
{
var variable = new Variable();
variable.Name = v.Name;
object variableValue = null;
inputs?.TryGetValue(v.Name, out variableValue);
variable.Value = GetVariableValue(document, schema, v, variableValue);
variables.Add(variable);
});
return variables;
}
示例3: GetVariableValues
public Variables GetVariableValues(ISchema schema, VariableDefinitions variableDefinitions, Inputs inputs)
{
var variables = new Variables();
variableDefinitions.Apply(v =>
{
var variable = new Variable();
variable.Name = v.Name;
object variableValue;
if (inputs != null && inputs.TryGetValue(v.Name, out variableValue))
{
var valueAst = variableValue.AstFromValue(schema, v.Type.GraphTypeFromType(schema));
variable.Value = GetVariableValue(schema, v, valueAst);
}
else
{
variable.Value = GetVariableValue(schema, v, v.DefaultValue);
}
variables.Add(variable);
});
return variables;
}