本文整理汇总了C#中Interpreter.SetVariable方法的典型用法代码示例。如果您正苦于以下问题:C# Interpreter.SetVariable方法的具体用法?C# Interpreter.SetVariable怎么用?C# Interpreter.SetVariable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Interpreter
的用法示例。
在下文中一共展示了Interpreter.SetVariable方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SystemExceptions_are_preserved_using_method_invocation
public void SystemExceptions_are_preserved_using_method_invocation()
{
var target = new Interpreter();
target.SetVariable("a", new MyTestService());
target.Eval("a.ThrowException()");
}
示例2: Can_use_overloaded_operators_on_class
public void Can_use_overloaded_operators_on_class()
{
var target = new Interpreter();
var x = new ClassWithOverloadedBinaryOperators(3);
target.SetVariable("x", x);
string y = "5";
Assert.IsFalse(x == y);
Assert.IsFalse(target.Eval<bool>("x == y", new Parameter("y", y)));
y = "3";
Assert.IsTrue(x == y);
Assert.IsTrue(target.Eval<bool>("x == y", new Parameter("y", y)));
Assert.IsFalse(target.Eval<bool>("x == \"4\""));
Assert.IsTrue(target.Eval<bool>("x == \"3\""));
Assert.IsTrue(!x == "-3");
Assert.IsTrue(target.Eval<bool>("!x == \"-3\""));
var z = new ClassWithOverloadedBinaryOperators(10);
Assert.IsTrue((x + z) == "13");
Assert.IsTrue(target.Eval<bool>("(x + z) == \"13\"", new Parameter("z", z)));
}
示例3: Registered_custom_identifiers_are_saved_inside_the_interpreter
public void Registered_custom_identifiers_are_saved_inside_the_interpreter()
{
var target = new Interpreter();
target.SetVariable("x", null);
Assert.IsTrue(target.Identifiers.Any(p => p.Name == "x"));
}
示例4: Simulate_a_working_context_using_this_keyword
public void Simulate_a_working_context_using_this_keyword()
{
var workingContext = new { FirstName = "homer" };
var interpreter = new Interpreter();
interpreter.SetVariable("this", workingContext);
Assert.AreEqual(workingContext.FirstName, interpreter.Eval("this.FirstName"));
}
示例5: Injection_a_property_expresion_to_simulate_a_working_context_parsing_another_property
public void Injection_a_property_expresion_to_simulate_a_working_context_parsing_another_property()
{
var workingContext = new { FirstName = "homer" };
var interpreter = new Interpreter();
interpreter.SetVariable("this", workingContext);
var firstNameExpression = interpreter.Parse("this.FirstName").LambdaExpression.Body;
interpreter.SetExpression("FirstName", firstNameExpression);
Assert.AreEqual(workingContext.FirstName, interpreter.Eval("FirstName"));
}
示例6: Unary_Cast_Operator
public void Unary_Cast_Operator()
{
var target = new Interpreter();
var x = 51.5;
target.SetVariable("x", x);
Assert.AreEqual((int)x, target.Eval("(int)x"));
Assert.AreEqual(typeof(int), target.Parse("(int)x").ReturnType);
Assert.AreEqual(typeof(object), target.Parse("(object)x").ReturnType);
Assert.AreEqual((double)84 + 9 * 8, target.Eval("(double)84 + 9 *8"));
}
示例7: CustomExceptions_WithoutSerializationConstructor_are_preserved
public void CustomExceptions_WithoutSerializationConstructor_are_preserved()
{
var target = new Interpreter();
Func<string> testException = new Func<string>(() =>
{
throw new MyException("Test");
});
target.SetVariable("testException", testException);
target.Eval("testException()");
}
示例8: Variables_can_be_overwritten_in_a_case_insensitive_setting
public void Variables_can_be_overwritten_in_a_case_insensitive_setting()
{
var target = new Interpreter(InterpreterOptions.DefaultCaseInsensitive)
.SetVariable("myk", 23);
Assert.AreEqual(23, target.Eval("myk"));
target.SetVariable("MYK", 3489);
Assert.AreEqual(3489, target.Eval("myk"));
Assert.AreEqual(typeof(int), target.Parse("myk").ReturnType);
}
示例9: Variables_can_be_overwritten
public void Variables_can_be_overwritten()
{
var target = new Interpreter()
.SetVariable("myk", 23);
Assert.AreEqual(23, target.Eval("myk"));
target.SetVariable("myk", 3489);
Assert.AreEqual(3489, target.Eval("myk"));
Assert.AreEqual(typeof(int), target.Parse("myk").ReturnType);
}
示例10: SystemExceptions_are_preserved_using_delegate_variable
public void SystemExceptions_are_preserved_using_delegate_variable()
{
var target = new Interpreter();
Func<string> testException = new Func<string>(() =>
{
throw new InvalidOperationException("Test");
});
target.SetVariable("testException", testException);
target.Eval("testException()");
}
示例11: RuleBuilder
public RuleBuilder(IpTablesSystem system, String nfbpf, Dictionary<int, IpTablesRuleSet> ruleSets, FunctionRegistry functions = null)
{
if (functions == null)
{
functions = new FunctionRegistry();
}
_system = system.System;
_nfbpf = nfbpf;
var chainsDict =
ruleSets.Select((a) => new KeyValuePair<int, IpTablesChainSet>(a.Key, a.Value.Chains))
.ToDictionary((a) => a.Key, (a) => a.Value);
_dcr = new DynamicChainRegister(system, chainsDict);
_formatDb = new DynamicDictionary<object>(_mappings);
_ruleSets = ruleSets;
_interpreter = new Interpreter();
_interpreter.SetVariable("var", _mappings);
functions.LoadFunctions(_interpreter);
}
示例12: Throw_an_exception_if_a_custom_type_doesnt_define_plus_operator
public void Throw_an_exception_if_a_custom_type_doesnt_define_plus_operator()
{
var target = new Interpreter();
var x = new TypeWithoutOverloadedBinaryOperators(3);
target.SetVariable("x", x);
int y = 5;
target.Parse("x + y", new Parameter("y", y));
}
示例13: Throw_an_exception_if_a_custom_type_doesnt_define_not_operator
public void Throw_an_exception_if_a_custom_type_doesnt_define_not_operator()
{
var target = new Interpreter();
var x = new TypeWithoutOverloadedBinaryOperators(3);
target.SetVariable("x", x);
target.Parse("!x");
}