简体   繁体   English

WinRT C#动态表达式评估器

[英]WinRT C# dynamic expression evaluator

There is a requirement in my application to evaluate string expressions during runtime in WinRT c# application. 我的应用程序中要求在WinRT c#应用程序的运行时期间评估字符串表达式。

Here are some example expressions: 以下是一些示例表达式:

strObj.Substring(10) + strObj.Substring(strObj.Length - 3) 
'001' + strObj.Substring(3) + '003'

Note: Above expressions will be defined in the backend and the application should evaluate at runtime with users input. 注意:以上表达式将在后端定义,应用程序应在运行时使用用户输入求值。

I looked at DynamicExpresso, NReco and some other expression evaluators none of those works in WinRT environment. 我查看了DynamicExpresso,NReco和其他一些表达式评估器,但这些都不在WinRT环境中起作用。 Is there any framework available in WinRT? WinRT中有可用的框架吗? or how can I achieve it in code? 或如何在代码中实现?

WinRT environment cannot reference usual class library until it compiled as portable class library (PCL). WinRT环境在编译为可移植类库(PCL)之前无法引用常规类库。 Portable libraries have a lot of restrictions on framework classes/methods usage (only subset of usual .NET classes is allowed); 可移植库对框架类/方法的使用有很多限制(仅允许使用普通.NET类的子集); in most cases class library cannot be compiled as PCL without additional adoption for these restrictions. 在大多数情况下,如果没有这些限制的进一步采用,则无法将类库编译为PCL。

I've investigated possibility of compiling NReco LambdaParser as PCL and got positive results. 我研究了将NReco LambdaParser编译为PCL的可能性,并获得了积极的结果。 PCL-adopted version doesn't include flexible NReco Type Converters (they are based on ITypeConverter/TypeDescriptor which are not available for PCL) and use just Convert.ChangeType instead. 采用PCL的版本不包括灵活的NReco类型转换器(它们基于ITypeConverter / TypeDescriptor,不适用于PCL),而仅使用Convert.ChangeType。

Download link: NReco LambdaParser Portable (with source code) 下载链接: NReco LambdaParser Portable(带有源代码)

The following code works fine with PCL version of LambdaParser: 以下代码可与LambdaParser的PCL版本配合使用:

var lambdaParser = new LambdaParser();
Func<string,int,string> left = (s,n) => { return s.Substring(n); };
var vars = new Dictionary<string,object>() {
    {"str1", "123456"},
    {"str2", "123"},
    {"Left", left} // custom function
};
var res = lambdaParser.Eval(
    "str1.Substring(3)+\" \"+str2.Substring(str2.Length-2)+\" \"+Left(str1,1)", vars );
Console.WriteLine("Res: {0}", res);

If you found that this PCL-version is usable I may publish its source code on GitHub and create Nuget package. 如果您发现此PCL版本可用,我可以在GitHub上发布其源代码并创建Nuget包。

You can also try DynLan - it also supports PCL / net3.5 / net.core ( https://github.com/byte/DynLan ). 您也可以尝试DynLan-它也支持PCL / net3.5 / net.core( https://github.com/byte/DynLan )。 The library parses the code itself and execute it line by line. 该库本身解析代码并逐行执行。 Here is small example with your expression (result == "klmnoprstuwxyzxyz"): 这是表达式的小示例(结果==“ klmnoprstuwxyzxyz”):

            var dict = new Dictionary<string, object>();
            dict["strObj"] = "abcdefghijklmnoprstuwxyz";

            object result = new Compiler().
                Compile(@" strObj.Substring(10) + strObj.Substring(strObj.Length - 3) ").
                Eval(dict);

You can also use variables inside you script, like that (result == "ABCDEPRSTU"): 您还可以在脚本中使用变量,例如(结果==“ ABCDEPRSTU”):

            var dict = new Dictionary<string, object>();
            dict["strObj"] = "abcdefghijklmnoprstuwxyz";

            object result = new Compiler().
                Compile(
                    @" a = strObj.Substring(0, 5).ToUpper(); " +
                    @" b = strObj.Substring(15, 5).ToUpper(); " +
                    @" a + b ").
                Eval(dict);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM