使用Microsoft Scripting Control来评估'If'表达式(通过c#) [英] Use Microsoft Scripting Control to evaluate 'If' expressions (via c#)
问题描述
使用MSScriptControl; //引用msscript.ocx
ScriptControlClass sc = new ScriptControlClass();
sc.Language =VBScript;
sc.AllowUI = true;
try
{
Console.WriteLine(sc.Eval(txtEx.Text).ToString());
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
(txtEx是一个简单的文本字段)
数值表达式:6 + 4,cos(34),abs(-99),round(1.234,2)等都很好
布尔表达式:true或false,1 = 2很好
但是如何评估一个简单的if?我试过if(true,2,3),iif(true,2,3),if(true)then 2 else 3和if(true)then 2 else 3 endif
有人可以帮我评估简单的条件表达式吗?任何帮助非常感谢!
RH
函数中的表达式
函数测试
if(true)then
return true
else
return false
end if
结束函数
添加函数到控件,然后使用运行
Result = ScriptControl.Run(Test)
(上面的代码没有测试,但是这样的东西应该可以工作)
查看此链接了解更多信息
http://support.microsoft.com/kb/184740
I have some c# code that uses the Microsoft Scripting Control to evaluate some expressions:
using MSScriptControl; // references msscript.ocx
ScriptControlClass sc = new ScriptControlClass();
sc.Language = "VBScript";
sc.AllowUI = true;
try
{
Console.WriteLine(sc.Eval(txtEx.Text).ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
(txtEx is a simple text field)
Numerical expressions: "6+4", "cos(34)", "abs(-99)", "round(1.234, 2)" etc. are fine
Boolean expressions: "true or false", "1=2" are fine
But how can I evaluate a simple 'if'? I have tried "if(true, 2, 3)", "iif(true, 2, 3)", "if (true) then 2 else 3" and "if (true) then 2 else 3 endif"
Can anybody please help me to evaluate simple conditional expressions? Any help much appreciated!
RH
Try wrapping your IF-expression in a function
Function test
if (true) then
return true
else
return false
end if
End function
Add the function to the control and then use Run
Result = ScriptControl.Run("Test")
(the code above is not tested, but something along that way should work)
Check out this link for some more info
http://support.microsoft.com/kb/184740
这篇关于使用Microsoft Scripting Control来评估'If'表达式(通过c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!