C#转换字符串用在逻辑条件 [英] C# convert a string for use in a logical condition

查看:141
本文介绍了C#转换字符串用在逻辑条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将一个字符串转换为一个运营商在一个逻辑条件使用。



例如

 如果(X Convert.ToOperator(>中)Y){} 

 如果(X>中为运营商Y){} 

我理解这可能不是标准的做法问题,所以我不感兴趣的答案是问我到底为什么会想。做这样的事情。



在此先感谢



编辑:好吧,我同意,才算公平给予一定的上下文。



我们有大约反思和XML构建系统。我希望能够这样说话,为便于。

 <值=Object1.Value操作员=>中条件=0/> 



编辑:感谢您的意见,我不能在这里正确解释上。我想我的问题是你不能,这是绝对正常(和我的想法)回答。 。感谢您的意见



编辑:草皮它我要一展身手



想象以下

 < Namespace.LogicRule值=Object1.Value操作员=>中条件=0> 

这将得到反射到一个类,所以现在我要测试的条件下,通过调用

 布尔LogicRule.Test()

那是有点哪里会都需要走到一起。



编辑:



确定,所以有没看过lambda表达式或表达式我想我会@ jrista的建议后,一起来看看。



我的系统允许枚举被解析,所以表达式有吸引力因为ExpressionType枚举的。



所以我创建了下面的类来验证这一想法:

 公共类操作
{
私有对象_right;
私有对象_Left;
私人ExpressionType _ExpressionType;
私人字符串_Type;

公共对象左
{
{返回_Left; }
集合{_Left =价值; }
}

公众反对的权利
{
{返回_right; }
集合{_right =价值; }
}

公共字符串类型
{
{返回_Type; }
集合{_Type =价值; }
}

公共ExpressionType ExpressionType
{
{返回_ExpressionType; }
集合{_ExpressionType =价值; }
}

公共BOOL评估()
{
VAR参数= Expression.Parameter(typeof运算(INT),左);
VAR参数2 = Expression.Parameter(typeof运算(INT),右);

表达式来; Func键< INT,INT,BOOL>>表达式= Expression.Lambda<&Func键LT; INT,INT,BOOL>>(
Expression.MakeBinary(ExpressionType,参数,参数2),参数,参数2);

Func键< INT,INT,BOOL>德尔= expression.Compile();

返回德尔(Convert.ToInt32(左),Convert.ToInt32(右));

}
}



显然,这将只的Int32工作现在的基本ExpressionTypes,我不知道我能做到通用?我以前从来没有使用表达式,然而这似乎工作。



这样就可以在我们的XML的方法声明为



 操作<左=1右=2ExpressionType =每种不超过TYPE =System.Int32/> 


解决方案

您可以做这样的事情:



 公共静态布尔比较< T>(字符串操作,T X,T y),其中T:IComparable的
{
开关(OP)
{
案==:返回,则x.compareTo(Y)== 0;
的情况下=!!返回则x.compareTo(Y)= 0;
案>中:返回,则x.compareTo(Y)大于0;
案> =:返回,则x.compareTo(Y)> = 0;
案< :返回,则x.compareTo(Y)℃的;
案< =:返回,则x.compareTo(Y)< = 0;
}
}


Is it possible to convert a string to an operator for use in a logical condition.

For example

if(x Convert.ToOperator(">") y) {}

or

if(x ">" as Operator y){}

I appreciate that this might not be standard practice question, therefore I'm not interested in answers that ask me why the hell would want to do something like this.

Thanks in advance

EDIT: OK I agree, only fair to give some context.

We have system built around reflection and XML. I would like to be able to say something like, for ease.

<Value = "Object1.Value" Operator = ">" Condition = "0"/>

EDIT: Thanks for your comments, I can't properly explain this on here. I guess my question is answered by "You can't", which is absolutely fine (and what I thought). Thanks for your comments.

EDIT: Sod it I'm going to have a go.

Imagine the following

<Namespace.LogicRule Value= "Object1.Value" Operator=">" Condition="0">

This will get reflected into a class, so now I want to test the condition, by calling

bool LogicRule.Test()

That's the bit where it would all need to come together.

EDIT:

OK, so having never looked at Lambdas or Expressions I thought I would have a look after @jrista's suggestions.

My system allows Enums to be parsed, so Expressions are attractive because of the ExpressionType Enum.

So I created the following class to test the idea:

    public class Operation
    {
        private object _Right;
        private object _Left;
        private ExpressionType _ExpressionType;
        private string _Type;

        public object Left
        {
            get { return _Left; }
            set { _Left = value; }
        }

        public object Right
        {
            get { return _Right; }
            set { _Right = value; }
        }

        public string Type
        {
            get { return _Type; }
            set { _Type = value; }
        }

        public ExpressionType ExpressionType
        {
            get { return _ExpressionType; }
            set { _ExpressionType = value; }
        }

        public bool Evaluate()
        {
            var param = Expression.Parameter(typeof(int), "left");
            var param2 = Expression.Parameter(typeof(int), "right");

            Expression<Func<int, int, bool>> expression = Expression.Lambda<Func<int, int, bool>>(
               Expression.MakeBinary(ExpressionType, param, param2), param, param2);

            Func<int, int, bool> del = expression.Compile();

            return del(Convert.ToInt32(Left), Convert.ToInt32(Right));

        }
    }

Obviously this will only work for Int32 right now and the basic ExpressionTypes, I'm not sure I can make it generic? I've never use Expressions before, however this seems to work.

This way can then be declared in our XML way as

Operation<Left="1" Right="2" ExpressionType="LessThan" Type="System.Int32"/>

解决方案

You could do something like this:

public static bool Compare<T>(string op, T x, T y) where T:IComparable
{
 switch(op)
 {
  case "==" : return x.CompareTo(y)==0;
  case "!=" : return x.CompareTo(y)!=0;
  case ">"  : return x.CompareTo(y)>0;
  case ">=" : return x.CompareTo(y)>=0;
  case "<"  : return x.CompareTo(y)<0;
  case "<=" : return x.CompareTo(y)<=0;
 }
}

这篇关于C#转换字符串用在逻辑条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆