zzuliulei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 // 示例:strFormULA = (a+b)*c 
 // 如果替换a=1 b=1 c=2 则结果为 (1+1)*2 = 4
 public int CalculateStringFormula(string strFormULA )
{
    var type = Type.GetTypeFromProgID("MSScriptControl.ScriptControl");
    var obj = Activator.CreateInstance(type);
    type.InvokeMember("Language", System.Reflection.BindingFlags.SetProperty, null, obj, new object[] { "javascript" });
    int result = Convert.ToInt32(type.InvokeMember("Eval", System.Reflection.BindingFlags.InvokeMethod, null, obj, new object[] { strFormULA }));
    return result;
}

posted on 2013-01-10 12:22  zzuliulei  阅读(323)  评论(0编辑  收藏  举报