using
System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace ConsoleApplication1
... {
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
int a = 100,b = 80, c =70;
string statement = string.Format("int a={0},b = {1},c = {0};", a, b, c);
string expressions = "(a + b) * 0.2 + c + (a - c * 0.1 )";
Console.WriteLine(GetValue(statement,expressions).ToString());
}
public static object GetValue(string statement, string expressions)
...{
string codeSnippet = "using System; " + " " +
"namespace SnippetCompiler {" + " " +
" public class Eval" + " " +
" {" + " " +
" public Eval(){} " + " " +
" public object GetValue()" + " " +
" {" + " " + statement + " " +
" return " + expressions + ";" + " " +
" }" + " " +
" } }";
CodeSnippetCompileUnit unit = new CodeSnippetCompileUnit(codeSnippet);
ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters para = new CompilerParameters();
para.ReferencedAssemblies.Add("System.dll");
para.GenerateInMemory = true;
para.GenerateExecutable = false;
para.OutputAssembly = "Eval.dll";
Assembly asm = compiler.CompileAssemblyFromDom(para, unit).CompiledAssembly;
Type type = asm.GetType("SnippetCompiler.Eval");
MethodInfo mi = type.GetMethod("GetValue", BindingFlags.Public | BindingFlags.Instance);
object obj = asm.CreateInstance("SnippetCompiler.Eval");
return mi.Invoke(obj, null);
}
}
}
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace ConsoleApplication1
... {
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
int a = 100,b = 80, c =70;
string statement = string.Format("int a={0},b = {1},c = {0};", a, b, c);
string expressions = "(a + b) * 0.2 + c + (a - c * 0.1 )";
Console.WriteLine(GetValue(statement,expressions).ToString());
}
public static object GetValue(string statement, string expressions)
...{
string codeSnippet = "using System; " + " " +
"namespace SnippetCompiler {" + " " +
" public class Eval" + " " +
" {" + " " +
" public Eval(){} " + " " +
" public object GetValue()" + " " +
" {" + " " + statement + " " +
" return " + expressions + ";" + " " +
" }" + " " +
" } }";
CodeSnippetCompileUnit unit = new CodeSnippetCompileUnit(codeSnippet);
ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters para = new CompilerParameters();
para.ReferencedAssemblies.Add("System.dll");
para.GenerateInMemory = true;
para.GenerateExecutable = false;
para.OutputAssembly = "Eval.dll";
Assembly asm = compiler.CompileAssemblyFromDom(para, unit).CompiledAssembly;
Type type = asm.GetType("SnippetCompiler.Eval");
MethodInfo mi = type.GetMethod("GetValue", BindingFlags.Public | BindingFlags.Instance);
object obj = asm.CreateInstance("SnippetCompiler.Eval");
return mi.Invoke(obj, null);
}
}
}