运营商在/包含规则引擎

时间:2014-06-04 03:28:06

标签: c#

我正在尝试为规则引擎实现IN运算符 LeftResult IN RightResult

object leftResult = methodInfo.Invoke(classInstance, leftParams.ToArray());
object rightResult = methodInfo.Invoke(classInstance, rightParams.ToArray());

leftResult是一个值列表,RightResult是一个逗号分隔的字符串。

 case Enumerations.ComparisonType.In:

                    if (leftResult != null && rightResult != null)
                    {
                        List<object> leftList;
                        List<object> rightList;
                        if (leftResult.ToString().Contains(","))
                        {
                            rightList = rightResult as List<object>;
                            if ((leftResult.ToString().Split(',')).Contains(rightList[0].ToString())) result = true;
                            else result = false;
                        }
                        else if (rightResult.ToString().Contains(","))
                        {
                            leftList = leftResult as List<object>;
                            if ((rightResult.ToString().Split(',')).Contains(leftList[0].ToString())) result = true;
                            else result = false;
                        }
                        else if (rightResult.GetType() == typeof(List<object>))
                        {

                            if ((rightResult as List<object>).Contains(leftResult)) result = true;
                            else result = false;
                        }
                        else if (leftResult.GetType() == typeof(List<object>))
                        {

                            if ((leftResult as List<object>).Contains(rightResult)) result = true;
                            else result = false;
                        }
                    }

这是我为处理左侧或右侧可能是列表或逗号分隔字符串的情况所做的代码。但它无法正常工作。任何人都可以建议一种更好的方法来实现规则引擎的In / Contains运算符

谢谢

1 个答案:

答案 0 :(得分:0)

我最喜欢的做这类测试的方法是使用HashSet<T>

http://msdn.microsoft.com/en-us/library/bb383091(v=vs.110).aspx

首先将您的两个数组/字符串转换为HashSet,然后根据您需要的逻辑使用IntersectWithOverlaps之类的内置方法。