DynamicExpresso表达式求值

DynamicExpresso 2.3.1 最低要求.net 4.6.1

github地址:https://github.com/davideicardi/DynamicExpresso

常用方法

/// <summary>
/// 忽略大小写判断相等
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static bool EqualsIgnorecase(object str1, object str2)
{
    if (str1 == null && str2 == null)
        return true;

    if (str1 == null || str2 == null)
    {
        return false;
    }

    return string.Equals(str1.ToString(), str2.ToString(), StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// str1 以 str2开头
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static bool StartsWithIgnorecase(object str1, object str2)
{
    if (str1 == null && str2 == null)
        return true;

    if (str1 == null || str2 == null)
    {
        return false;
    }

    return str1.ToString().StartsWith(str2.ToString(), StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// str1 以 str2 结尾
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static bool EndsWithIgnorecase(object str1, object str2)
{
    if (str1 == null && str2 == null)
        return true;

    if (str1 == null || str2 == null)
    {
        return false;
    }

    return str1.ToString().EndsWith(str2.ToString(), StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// 忽略大小写 比较
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static int CompareIgnorecase(object str1, object str2)
{
    if (str1 == null && str2 == null)
        return 0;

    if (str1 == null || str2 == null)
    {
        return 0;
    }

    return string.Compare(str1.ToString(), str2.ToString(), StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// 索引位置
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static int IndexOfIgnorecase(object str1, object str2)
{
    if (str1 == null && str2 == null)
        return -1;

    if (str1 == null || str2 == null)
    {
        return -1;
    }

    return str1.ToString().IndexOf(str2.ToString(), StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// 匹配最后一个索引位置
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
public static int LastIndexOfIgnorecase(object str1, object str2)
{
    if (str1 == null && str2 == null)
        return -1;

    if (str1 == null || str2 == null)
    {
        return -1;
    }

    return str1.ToString().LastIndexOf(str2.ToString(), StringComparison.OrdinalIgnoreCase);
}

扩展string

/// <summary>
/// 解析表达式
/// </summary>
/// <param name="str">字符串</param>
/// <param name="paramName">参数名</param>
/// <param name="obj">参数</param>
/// <returns></returns>
public static T Eval<T>(this string str, string paramName = "obj", object obj = null)
{
    Interpreter expr = new Interpreter(InterpreterOptions.Default)
        .SetFunction("equalsIgnore", (Func<object, object, bool>)EqualsIgnorecase)
        .SetFunction("startsIgnore", (Func<object, object, bool>)StartsWithIgnorecase)
        .SetFunction("endsIgnore", (Func<object, object, bool>)EndsWithIgnorecase)
        .SetFunction("compareIgnore", (Func<object, object, int>)CompareIgnorecase)
        .SetFunction("indexOfIgnore", (Func<object, object, int>)IndexOfIgnorecase)
        .SetFunction("lastIndexOfIgnore", (Func<object, object, int>)LastIndexOfIgnorecase);
    if (obj != null)
    {
        paramName = paramName ?? "obj";
        expr.SetVariable(paramName, obj);
    }
    return expr.Eval<T>(str);
}

测试:

using DynamicExpresso;
using System;
using System.Collections.Generic;
using System.Dynamic;

namespace Test
{
    class Program
    {
        static void Main()
        {
            int result = "3+2".Eval<int>();
            Console.WriteLine(result);

            dynamic obj = new ExpandoObject();
            obj.Name = ".net";
            obj.Id = 10;

            IDictionary<string, object> dic = obj as IDictionary<string, object>;
            dic.Add("Address", "北京市东城区");
            /*
            var interpreter = new Interpreter()
                .SetVariable("obj", (object)obj);
            Console.WriteLine(obj.Id == interpreter.Eval<int>("obj.Id"));
            */

            //True
            bool flag = "((int)obj.Id) > 2 && equalsIgnore(obj.Name,\".Net\") && startsIgnore(obj.Name,\".\")"
                .Eval<bool>("obj", (object)obj);
            Console.WriteLine(flag);

            //True
            flag = "obj.Address.Contains(\"北京\")".Eval<bool>("obj", (object)obj);
            Console.WriteLine(flag);
            Console.ReadLine();
        }
    }
}

结果(4.7.1,4.6.1一样)

发布了23 篇原创文章 · 获赞 4 · 访问量 5万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览