本文整理汇总了C#中Interpreter.ParseAsDelegate方法的典型用法代码示例。如果您正苦于以下问题:C# Interpreter.ParseAsDelegate方法的具体用法?C# Interpreter.ParseAsDelegate怎么用?C# Interpreter.ParseAsDelegate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Interpreter
的用法示例。
在下文中一共展示了Interpreter.ParseAsDelegate方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parse_To_a_Delegate_With_No_Parameters
public void Parse_To_a_Delegate_With_No_Parameters()
{
var target = new Interpreter();
var func = target.ParseAsDelegate<Func<int>>("50");
Assert.AreEqual(50, func());
}
示例2: Parse_To_a_Delegate
public void Parse_To_a_Delegate()
{
var target = new Interpreter();
var func = target.ParseAsDelegate<Func<double, double, double>>("Math.Pow(x, y) + 5", "x", "y");
Assert.AreEqual(Math.Pow(10, 2) + 5, func(10, 2));
}
示例3: Parse_To_a_Custom_Delegate
public void Parse_To_a_Custom_Delegate()
{
var target = new Interpreter();
var func = target.ParseAsDelegate<MyCustomDelegate>("x + y.Length");
Assert.AreEqual(7, func(3, "ciao"));
Assert.AreEqual(10, func(5, "mondo"));
}
示例4: Parse_To_a_Delegate_With_Two_Parameters
public void Parse_To_a_Delegate_With_Two_Parameters()
{
var target = new Interpreter();
var func = target.ParseAsDelegate<Func<double, double, double>>("arg1 * arg2");
Assert.AreEqual(6, func(3, 2));
Assert.AreEqual(50, func(5, 10));
}
示例5: Parse_To_a_Delegate_With_One_Parameter
public void Parse_To_a_Delegate_With_One_Parameter()
{
var target = new Interpreter();
var func = target.ParseAsDelegate<Func<string, int>>("arg.Length");
Assert.AreEqual(4, func("ciao"));
Assert.AreEqual(9, func("123456879"));
}
示例6: Parse_To_a_Delegate_With_Two_Parameters_With_Custom_Name
public void Parse_To_a_Delegate_With_Two_Parameters_With_Custom_Name()
{
var target = new Interpreter();
var argumentNames = new string[] { "x", "y" };
var func = target.ParseAsDelegate<Func<double, double, double>>("x * y", argumentNames);
Assert.AreEqual(6, func(3, 2));
Assert.AreEqual(50, func(5, 10));
}
示例7: Parse_To_a_Delegate_With_One_Parameter_With_Custom_Name
public void Parse_To_a_Delegate_With_One_Parameter_With_Custom_Name()
{
var target = new Interpreter();
var argumentName = "val"; // if not specified the delegate parameter is used which is "arg"
var func = target.ParseAsDelegate<Func<string, int>>("val.Length", argumentName);
Assert.AreEqual(4, func("ciao"));
Assert.AreEqual(9, func("123456879"));
}
示例8: Linq_Where
public void Linq_Where()
{
var customers = new List<Customer> {
new Customer() { Name = "David", Age = 31, Gender = 'M' },
new Customer() { Name = "Mary", Age = 29, Gender = 'F' },
new Customer() { Name = "Jack", Age = 2, Gender = 'M' },
new Customer() { Name = "Marta", Age = 1, Gender = 'F' },
new Customer() { Name = "Moses", Age = 120, Gender = 'M' },
};
string whereExpression = "customer.Age > 18 && customer.Gender == 'F'";
var interpreter = new Interpreter();
Func<Customer, bool> dynamicWhere = interpreter.ParseAsDelegate<Func<Customer, bool>>(whereExpression, "customer");
Assert.AreEqual(1, customers.Where(dynamicWhere).Count());
}
示例9: Implicit_conversion_operator_for_lambda
public void Implicit_conversion_operator_for_lambda()
{
var target = new Interpreter()
.SetVariable("x", new TypeWithImplicitConversion(10));
var func = target.ParseAsDelegate<Func<int>>("x");
int val = func();
Assert.AreEqual(10, val);
}
示例10: Interpreter
public void When_parsing_an_expression_to_a_delegate_the_delegate_parameters_are_respected_also_if_the_expression_doesnt_use_it()
{
var target = new Interpreter();
var myDelegate = target.ParseAsDelegate<TestDelegate>("x + y");
// parameter 'z' is not used but the delegate accept it in any case without problem
Assert.AreEqual(3, myDelegate(1, 2, 123123));
Assert.AreEqual(24, myDelegate(21, 3, 433123));
}
示例11: Return_Type_Mismatch_Cause_An_Exception
public void Return_Type_Mismatch_Cause_An_Exception()
{
var target = new Interpreter();
// expected a double but I return a string
target.ParseAsDelegate<Func<double>>("\"ciao\"");
}