using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.Library.Utils.SereinExpression.Resolver
{
public class StringConditionResolver : SereinConditionResolver
{
public enum Operator
{
///
/// 出现过
///
Contains,
///
/// 没有出现过
///
DoesNotContain,
///
/// 相等
///
Equal,
///
/// 不相等
///
NotEqual,
///
/// 起始字符串等于
///
StartsWith,
///
/// 结束字符串等于
///
EndsWith
}
public Operator Op { get; set; }
public string Value { get; set; }
public override bool Evaluate(object obj)
{
if (obj is string strObj)
{
/*return Op switch
{
Operator.Contains => strObj.Contains(Value),
Operator.DoesNotContain => !strObj.Contains(Value),
Operator.Equal => strObj == Value,
Operator.NotEqual => strObj != Value,
Operator.StartsWith => strObj.StartsWith(Value),
Operator.EndsWith => strObj.EndsWith(Value),
_ => throw new NotSupportedException("不支持的条件类型"),
};*/
switch (Op)
{
case Operator.Contains:
return strObj.Contains(Value);
case Operator.DoesNotContain:
return !strObj.Contains(Value);
case Operator.Equal:
return strObj == Value;
case Operator.NotEqual:
return strObj != Value;
case Operator.StartsWith:
return strObj.StartsWith(Value);
case Operator.EndsWith:
return strObj.EndsWith(Value);
default:
throw new NotSupportedException("不支持的条件类型");
}
}
return false;
}
}
}