C#动态条件 [英] C# dynamic condition

查看:153
本文介绍了C#动态条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景需要针对类对象属性评估动态条件。

示例:我有一个Person类,它有一些属性Age,Name,City,Pincode等。

现在从文本框值开始,我将得到字符串条件,如(年龄= 23,城市=伦敦或Pincode = 123)。这些条件是动态的。我需要针对人类对象检查这些条件。

考试:

if(objPerson.Age == 23&& objPerson.city == London || objPerson.Pincode == 123)

做一些事情



有没有一种简单的方法可以在运行时根据person类对象属性值来评估这些条件。



我的尝试:



............. .................................................. ....................................

I have a scenario where I need to evaluate a dynamic condition against the class object property.
Example :I have a Person class which have some property Age,Name,City,Pincode etc.
now from text box value ,I will get the string condition like (Age=23 and City=London or Pincode =123).These condition is dynamic.i need to check these condition against the person class object.
exam:
if(objPerson.Age==23 && objPerson.city==London ||objPerson.Pincode ==123)
do some thing

Is there an easy way to evaluate these condition at run time against the person class object property value.

What I have tried:

...................................................................................................

推荐答案

这可能会有所帮助:



CSharpCodeProvider Class(Microsoft.CSharp)| Microsoft Docs [ ^ ]



当然,你必须用字符串中相应的C#运算符替换和和或首先...



编辑==========================


我也发现了这个(这可能更合适):<​​br />


GitHub - davideicardi / DynamicExpresso:C#表达式解释器 [ ^ ]



了解如何使用Google,或者您作为开发人员的职业生涯将会很短。
This may help:

CSharpCodeProvider Class (Microsoft.CSharp) | Microsoft Docs[^]

Of course, you have to replace " and " and " or " with their appropriate C# operators in the string first...

EDIT ==========================

I also found this (which might be more appropriate):

GitHub - davideicardi/DynamicExpresso: C# expressions interpreter[^]

Learn how to use Google, or your career as a developer will be short.


这篇关于C#动态条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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