是否可以编译和运行时在.NET执行新的code? [英] Is it possible to compile and execute new code at runtime in .NET?

查看:268
本文介绍了是否可以编译和运行时在.NET执行新的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:数学EX pression评价不是这个问题的焦点。我想要编译并在运行时在.NET执行新的code。话虽这么说......

Note: Mathematical expression evaluation is not the focus of this question. I want to compile and execute new code at runtime in .NET. That being said...

我想允许用户输入任何方程,像下文中,到文本框:

I would like to allow the user to enter any equation, like the following, into a text box:

x = x / 2 * 0.07914
x = x^2 / 5

和必须应用于传入的数据点的方程。传入的​​数据点被重新通过的 X 并每个数据点是由用户指定的方程处理psented $ P $。我这样做几年前,但我不喜欢的解决方案,因为它需要解析式的文本为每一个计算:

And have that equation applied to incoming data points. The incoming data points are represented by x and each data point is processed by the user-specified equation. I did this years ago, but I didn't like the solution because it required parsing the text of the equation for every calculation:

float ApplyEquation (string equation, float dataPoint)
{
    // parse the equation string and figure out how to do the math
    // lots of messy code here...
}

当你正在处理的数据点的容载量,这将引入相当多的开销。我想能够等式转化为一个函数,在飞行,因此,它仅需要一次解析。它看起来是这样的:

When you're processing boatloads of data points, this introduces quite a bit of overhead. I would like to be able to translate the equation into a function, on the fly, so that it only has to be parsed once. It would look something like this:

FunctionPointer foo = ConvertEquationToCode(equation);
....
x = foo(x);  // I could then apply the equation to my incoming data like this

功能ConvertEquationTo code将解析方程,并返回一个指向应用适当的数学函数。

Function ConvertEquationToCode would parse the equation and return a pointer to a function that applies the appropriate math.

该应用程序将基本被写入新的code运行时。使用.NET这可能吗?

The app would basically be writing new code at run time. Is this possible with .NET?

推荐答案

是的!使用在 Microsoft.CSharp ,<一个href="http://msdn.microsoft.com/en-us/library/system.$c$cdom.compiler.aspx">System.$c$cDom.Compiler,和的System.Reflection 的名称空间。下面是一个简单的控制台应用程序编译的类(SomeClass的)有一个方法(Add42),然后,您可以调用该方法。这是一个最基本的例子,我格式化下降到prevent滚动条出现在code显示。这只是证明编译和使用新的code运行时。

Yes! Using methods found in the Microsoft.CSharp, System.CodeDom.Compiler, and System.Reflection name spaces. Here is a simple console app that compiles a class ("SomeClass") with one method ("Add42") and then allows you to invoke that method. This is a bare-bones example that I formatted down to prevent scroll bars from appearing in the code display. It is just to demonstrate compiling and using new code at run time.

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

namespace RuntimeCompilationTest {
    class Program
    {
        static void Main(string[] args) {
            string sourceCode = @"
                public class SomeClass {
                    public int Add42 (int parameter) {
                        return parameter += 42;
                    }
                }";
            var compParms = new CompilerParameters{
                GenerateExecutable = false, 
                GenerateInMemory = true
            };
            var csProvider = new CSharpCodeProvider();
            CompilerResults compilerResults = 
                csProvider.CompileAssemblyFromSource(compParms, sourceCode);
            object typeInstance = 
                compilerResults.CompiledAssembly.CreateInstance("SomeClass");
            MethodInfo mi = typeInstance.GetType().GetMethod("Add42");
            int methodOutput = 
                (int)mi.Invoke(typeInstance, new object[] { 1 }); 
            Console.WriteLine(methodOutput);
            Console.ReadLine();
        }
    }
}

这篇关于是否可以编译和运行时在.NET执行新的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆