c#动态脚本

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");
    string outstr = "";
    for (int i = 1; i < 100; i++)
    {
        object[] vParams = { 1, i };
        object vDouble = vTest.Invoke(vTemp, vParams);
        outstr += vDouble.ToString() + "\r\n";
    }
    Console.WriteLine(outstr);

猜你喜欢

转载自blog.csdn.net/nishuodeqianshou/article/details/78686070