动态计算字符串表达式的结果 [英] calculate result from a string expression dynamically

查看:61
本文介绍了动态计算字符串表达式的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以计算字符串表达式的结果,例如
mystring = 2 * a + 32-Math.Sin(6) 动态地知道 a 是我拥有的一个变量,可能有一些动态的解决方案,或者正在使用 System.Reflection

Is there a way to calculate the result of a string expression, for example mystring = "2*a+32-Math.Sin(6)" dynamically knowing that a is a variable that I have, may be there is some dynamic solution or using System.Reflection

string mystring = "2*a+32-Math.Sin(6)"`;
decimal result = SomeMethod(mystring,3); // where a = 3 for example


推荐答案

javascript计算您的表达式?

How about making javascript calculate your expression?

Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));

dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "javascript";

var res = obj.Eval("a=3; 2*a+32-Math.sin(6)");

这篇关于动态计算字符串表达式的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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