明辉手游网中心:是一个免费提供流行视频软件教程、在线学习分享的学习平台!

通过动态编译取得字符串表达的值

[摘要]看到许多人经常问到这个问题: 怎么由字符串 “126 + (256 - 2^4 )”,或者怎么判断 “115 > 56 14<45”的结果等等,在MSDN上查了查,写了一个Eval类 /***********************************************...
看到许多人经常问到这个问题: 怎么由字符串 “126 + (256 - 2^4 )”,或者怎么判断 “115 > 56 14<45”的结果等等,在MSDN上查了查,写了一个Eval类


/*****************************************************************
** 文件名:       Eval.cs
** Copyright (c) 1999 -2003
** 创建人:       Phoenix
** 创建日期:
** 修改人:
** 修改日期:
** 描 述:         获取字符串所表示的逻辑意义
** 版 本:1.0
******************************************************************/

using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;

public class Eval
{
static object GetValue( string value )
 {
   string codeSnippet = "using System; " + "\r\n" +
                                   "namespace CzG {" + "\r\n" +
                                               " public class Eval" + "\r\n" +
                                               " {" + "\r\n" +
                                   "       public Eval(){} " + "\r\n" +
                                               "  public object GetValue()" + "\r\n" +
                                               "  {" + "\r\n" +
                                               "   return " + value + ";" + "\r\n" +
                                               "  }" + "\r\n" +
                                               " } }";