明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

通過動(dòng)態(tài)編譯取得字符串表達(dá)的值

[摘要]看到許多人經(jīng)常問到這個(gè)問題: 怎么由字符串 “126 + (256 - 2^4 )”,或者怎么判斷 “115 > 56 14<45”的結(jié)果等等,在MSDN上查了查,寫了一個(gè)Eval類 /***********************************************...
看到許多人經(jīng)常問到這個(gè)問題: 怎么由字符串 “126 + (256 - 2^4 )”,或者怎么判斷 “115 > 56 14<45”的結(jié)果等等,在MSDN上查了查,寫了一個(gè)Eval類


/*****************************************************************
** 文件名:       Eval.cs
** Copyright (c) 1999 -2003
** 創(chuàng)建人:       Phoenix
** 創(chuàng)建日期:
** 修改人:
** 修改日期:
** 描 述:         獲取字符串所表示的邏輯意義
** 版 本: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" +
                                               " } }";