我有一个业务逻辑,基于预定义的条件(OperatorType
和比较值),验证它是真还是假。这里的问题是值是“键入的”,并且带有运算符(大于/小于/等于等)。
如果ValueType
是Integer,则需要Integer
先将值和比较值转换成,然后根据OperatorType
,进行比较。
我想知道我是否可以有一个通用函数,传递CompareValue
, Value
, ValueType
,OperatorType
并返回真/假。
using System;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var compare1 = 3;
var foo1 = new Foo { Value = "2", OperatorType = OperatorTypes.GreaterOrEqual, ValueType = ValueTypes.Long };
//compare compare1 and foo1.Value, output should be false (2 < 3)
var compare2 = true;
var foo2 = new Foo { Value = "True", OperatorType = OperatorTypes.Equal, ValueType = ValueTypes.Bool };
//compare compare2 and foo2.Value, output should be true (true = true)
var compare3 = DateTime.Parse("2013-03-19 15:00");
var foo3 = new Foo { Value = "2013-03-19 16:00", OperatorType = OperatorTypes.Less, ValueType = ValueTypes.Date };
//compare compare3 and foo3.Value, output should be false (2013-03-19 16:00 < 2013-03-19 15:00)
}
}
public enum OperatorTypes : uint
{
Equal = 1,
Greater = 2,
GreaterOrEqual = 3,
Less = 4,
LessOrEqual = 5
}
public enum ValueTypes : uint
{
None = 0,
Integer = 1,
Long = 2,
Numeric = 3,
Date = 4,
Text = 5,
Bool = 6
}
class Foo
{
public string Value { get; set; }
public ValueTypes ValueType { get; set; }
public OperatorTypes OperatorType { get; set; }
}
}