陋室铭
永远也不要停下学习的脚步(大道至简至易)

using Microsoft.CSharp;

using System.CodeDom.Compiler;

using System.Reflection;

 

private void button1_Click(object sender, EventArgs e)

{

    ICodeCompiler vCodeCompiler = new CSharpCodeProvider().CreateCompiler();

    CompilerParameters vCompilerParameters = new CompilerParameters();

    vCompilerParameters.GenerateExecutable = false;

    vCompilerParameters.GenerateInMemory = true;

    string vSource =

        "public class Temp"n" +

        "{"n" +

          public double Test(double A, double B)"n" +

          {"n" +

              return A / B;"n" +

          }"n" +

        "}"n";

    CompilerResults vCompilerResults =

        vCodeCompiler.CompileAssemblyFromSource(vCompilerParameters, vSource);

    Assembly vAssembly = vCompilerResults.CompiledAssembly;

    object vTemp = vAssembly.CreateInstance("Temp");

    MethodInfo vTest = vTemp.GetType().GetMethod("Test");

    for (int i = 1; i < 100; i++)

    {

        object[] vParams = { 1, i };

        object vDouble = vTest.Invoke(vTemp, vParams);

        textBox1.AppendText(vDouble.ToString() + ""r"n");

    }

}

 

posted on 2008-08-06 14:58  宏宇  阅读(3952)  评论(3编辑  收藏  举报