Eval.cs的改进版 动态编译类,可以计算表达式,也可以调用系统中的类
/*
動態編譯類,可以計算表達式,也可以調用系統中的類
調用方式: ????return new XXXXXXX.Eval().GetValue("System.DateTime.Now")
返回結果:???? 2005-08-04 15:00:24
*/
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.IO;
namespace XXXXXXX
{
?/*****************************************************************
?** 文件名:?????? Eval.cs
?** Copyright (c) 1999 -2003
?** 創建人:?????? Phoenix
?** 創建日期:
?** 修改人:?? BigmouthZ@163.net
?** 修改日期: 2005/08/04
?** 描 述:???????? 獲取字符串所表示的邏輯意義
?** 版 本:1.0
?******************************************************************/
?public class Eval
?{
??public object GetValue( string value )
??{
???string codeSnippet =
@"
using System;
using System.Collections;
using DBCore;
namespace CzG
{
?public class $0$
?{
??public $0$(){}
??public object GetValue()
??{
???return $1$ ;
??}
?}
}";
???string tmp = "Eval" + Convert.ToString(System.DateTime.Now.Ticks);
???codeSnippet = codeSnippet.Replace("$0$",tmp);
???codeSnippet = codeSnippet.Replace("$1$",value);
???object b = "";
???try
???{
????ICodeCompiler compiler =? new CSharpCodeProvider().CreateCompiler();
????CompilerParameters para = new CompilerParameters();
????para.ReferencedAssemblies.Add( "System.dll" );
????string path = System.Environment.CurrentDirectory;
para.ReferencedAssemblies.Add( path + @"\DBCore.dll" );
????para.GenerateInMemory = true;
????para.GenerateExecutable = false;
????para.OutputAssembly = path + "\\" + tmp + ".dll";
????CompilerResults cr = compiler.CompileAssemblyFromSource(para, codeSnippet);
????Assembly asm = cr.CompiledAssembly;
??
????object obj = asm.CreateInstance( "CzG." + tmp );
????Type type = asm.GetType( "CzG." + tmp );
????MethodInfo mi = type.GetMethod("GetValue");?
?? ?
????b =? mi.Invoke( obj , null );
????GC.Collect();
????System.IO.File.Delete( path + "\\" + tmp + ".dll");
???}
???catch
???{
????b = "CallError!";
???}
???return b;
??}
?}
}
轉載于:https://www.cnblogs.com/bigmouthz/archive/2006/01/24/322546.html
總結
以上是生活随笔為你收集整理的Eval.cs的改进版 动态编译类,可以计算表达式,也可以调用系统中的类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用户NT AUTHORITYNETWOR
- 下一篇: 为什么本地sqlservr.exe进程占