Составители:
Рубрика:
}
class MainClass
{
static Function CompileToCS(Expression expr)
{
ICodeCompiler compiler =
new CSharpCodeProvider().CreateCompiler();
CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add(“System.dll”);
parameters.ReferencedAssemblies.Add(“Integral.exe”);
parameters.GenerateInMemory = true;
string e = expr.GenerateCS();
string code =
“public class FunctionCS: Function\n”+
“{\n”+
“ public override double Eval(double x)\n”+
“ {\n”+
“ return “+e+”;\n”+
“ }\n”+
“}\n”;
CompilerResults compilerResults =
compiler.CompileAssemblyFromSource(parameters,code);
Assembly assembly = compilerResults.CompiledAssembly;
return assembly.CreateInstance(“FunctionCS”) as Function;
}
static Function CompileToCIL(Expression expr)
{
AppDomain appDomain = Thread.GetDomain();
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = “f”;
AssemblyBuilder assembly =
appDomain.DefineDynamicAssembly(
assemblyName,
AssemblyBuilderAccess.RunAndSave
);
ModuleBuilder module =
Исходный код программы Integral
309
private bool isMulOp()
{
return Regex.IsMatch(token.Value,”\\*|/”);
}
}
B.2. Integral.cs
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using Microsoft.CSharp;
public abstract class Function
{
public abstract double Eval(double x);
}
public class TestFunction: Function
{
public override double Eval(double x)
{
return x * Math.Sin(x);
}
}
public class InterpretingFunction: Function
{
private Expression expr;
public InterpretingFunction(Expression expr)
{
this.expr = expr;
}
public override double Eval(double x)
{
return expr.Evaluate(x);
}
308
CIL и системное программирование в Microsoft .NET
308 CIL и системное программирование в Microsoft .NET Исходный код программы Integral 309 private bool isMulOp() } { return Regex.IsMatch(token.Value,”\\*|/”); class MainClass } { } static Function CompileToCS(Expression expr) { ICodeCompiler compiler = B.2. Integral.cs new CSharpCodeProvider().CreateCompiler(); CompilerParameters parameters = new CompilerParameters(); using System; parameters.ReferencedAssemblies.Add(“System.dll”); using System.CodeDom.Compiler; parameters.ReferencedAssemblies.Add(“Integral.exe”); using System.Reflection; parameters.GenerateInMemory = true; using System.Reflection.Emit; string e = expr.GenerateCS(); using System.Threading; string code = using Microsoft.CSharp; “public class FunctionCS: Function\n”+ “{\n”+ public abstract class Function “ public override double Eval(double x)\n”+ { “ {\n”+ public abstract double Eval(double x); “ return “+e+”;\n”+ } “ }\n”+ “}\n”; public class TestFunction: Function { CompilerResults compilerResults = public override double Eval(double x) compiler.CompileAssemblyFromSource(parameters,code); { return x * Math.Sin(x); Assembly assembly = compilerResults.CompiledAssembly; } return assembly.CreateInstance(“FunctionCS”) as Function; } } public class InterpretingFunction: Function static Function CompileToCIL(Expression expr) { { private Expression expr; AppDomain appDomain = Thread.GetDomain(); AssemblyName assemblyName = new AssemblyName(); public InterpretingFunction(Expression expr) assemblyName.Name = “f”; { this.expr = expr; AssemblyBuilder assembly = } appDomain.DefineDynamicAssembly( assemblyName, public override double Eval(double x) AssemblyBuilderAccess.RunAndSave { ); return expr.Evaluate(x); } ModuleBuilder module =
Страницы
- « первая
- ‹ предыдущая
- …
- 159
- 160
- 161
- 162
- 163
- …
- следующая ›
- последняя »