如何在c#中对字符串中的变量进行评估 [英] how to evalueate variables inside strings in c#
问题描述
您好,
是否可以在c#.............
string a = 10;
string b = 20;
string c = 40;
k =a * b + c;
m =a + b + c;
n =a * b * c
string output = evaluate (k)//输出= 240
字符串输出=评估(m)//输出= 70
字符串输出=评估(n)//输出= 800
如果是,请帮我在c#中做到这一点...
变量在运行时声明
,k,m,n的公式在数据库中被sroted。并在运行时获取。
=============================
实际情况如下: - 我有几个变量存储在表格中。
table1:fieldmaster
field_name | field_type | field_length
ab varchar 20
bc varchar 20
ca varchar 20
我在表中获取所有field_name 。
并拿了一本字典并添加了field_name作为字典键。
和for循环为每个键分配值
----
现在在其他表中我有一些评估字段的公式。
table2:formulamast
field_name |公式
ca | ab +(bc * bc)
现在我必须根据公式表中的公式来计算ca的值。
Hello,
Is it possible in c#.............
string a = "10";
string b = 20;
string c = 40;
k="a*b+c";
m="a+b+c";
n="a*b*c"
string output = evaluate(k) // output =240
string output = evaluate(m) // output =70
string output = evaluate(n) // output =800
if yes please help me how do this in c#...
variable are declared at runtime
and formula of k,m,n is sroted in database. and fetch at runtime.
=============================
actually the real scenario is as :- i have several variables stored in table.
table1:fieldmaster
field_name | field_type | field_length
ab varchar 20
bc varchar 20
ca varchar 20
and i fetch all field_name in a table.
and took a dictionary and add field_name as dictionary key.
and in for loop assign values to each key
----
now in other table i have some formula which evaluate fields.
table2 : formulamast
field_name | formula
ca | ab+(bc*bc)
now i have to evaluate values of ca according to formula which is in formula table.
推荐答案
这可能帮助:
逃离 - 快速轻量级表达评估器 [ ^ ]
请尝试以下代码
Please try below code
try
{
string Expr = "(1+1+1)*1";
var result = new DataTable().Compute(Expr, null);
}
catch { }
< br $> b $ b
希望这能解决你的目的。
Hope this will solve your purpose.
这篇关于如何在c#中对字符串中的变量进行评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!