运营商作为字符串 [英] operators as strings

查看:197
本文介绍了运营商作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要评估工科数学EX pression是psented我在C#中的字符串$ P $。例如诺迪但得到的点跨字符串作为前pression。

I need to evaluate a mathmatical expression that is presented to me as a string in C#. Example noddy but gets the point across that the string as the expression.

我需要的评估,以然后填充一个int。

I need the evaluate to then populate an int.

有没有eval()函数在C#一样在别人langugaes ...

There is no Eval() in C# like in others langugaes...

String myString = "3*4";

编辑:

我在VS2008

试过Microsoft.JScript程序。 =它去precated方法(但仍符合 - 警告)

Tried the Microsoft.JScript. = Its deprecated method (but still complies - warning)

然而,Microsoft.JScript程序的DLL我有多恩斯上工作

However the Microsoft.JScript dll that I have doens work on

公共对象InvokeMember(串
  名的BindingFlags invokeAttr,捆扎机
  粘结剂,目标对象,对象[]参数);

public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args);

诉说有一个缺失;去图...

Complains that there is a missing ";" go figure...

编辑2

解决方案 - 是codeDOM之一 - 它工作了,因为没有安全问题 - 只有我永远要运行的code。非常感谢您的答复...

Solution - was the codeDom one - it worked for as there are no security issue - only me ever going to be running the code. Many thanks for the replies ...

和链接到新的龙书真棒

编辑3

马特dataTable.Compute()也适用 - 即使对于安全意识更好。 (参数检查说明)

Matt dataTable.Compute() also works - even better for the security conscious. (parameter checking noted)

推荐答案

我看到它的方式,你有两个选择 - 使用前pression评估或构建,编译
和动态运行C#code。

The way I see it, you have two options - use an expression evaluator or construct, compile and run C# code on the fly.

我会和前pression评估图书馆去,因为你不必担心任何安全问题。也就是说,你可能不能够使用在中等信任环境code一代,如大多数共享托管服务器。

I would go with an expression evaluator library, as you do not have to worry about any security issues. That is, you might not be able to use code generation in medium trust environments, such as most shared hosting servers.

下面是产生code,以评估前pressions一个例子:
http://www.vbforums.com/showthread.php?t=397264

Here is an example for generating code to evaluate expressions: http://www.vbforums.com/showthread.php?t=397264

这篇关于运营商作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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