首页  编辑  

动态执行C#语句

Tags: /C#/RTTI/   Date Created:

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" );

   }

}

//

private void button1_Click( object sender , EventArgs e)

{

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

    CompilerParameters vCompilerParameters = new CompilerParameters ();

   vCompilerParameters . GenerateExecutable = false ;

   vCompilerParameters . GenerateInMemory = true ;

   vCompilerParameters . ReferencedAssemblies . Add( "System.Windows.Forms.dll" ); //

    string vSource =

        "using System.Windows.Forms;\n" +

        "public class Temp\n" +

        "{\n" +

        "    public void Test()\n" +

        "    {\n" +

        "         MessageBox.Show(\"Hello World!\");\n" +

        "    }\n" +

        "}\n" ;

    CompilerResults vCompilerResults =

       vCodeCompiler . CompileAssemblyFromSource(vCompilerParameters, vSource);

   

    Assembly vAssembly = vCompilerResults . CompiledAssembly;

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

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

   vTest . Invoke(vTemp, null );

}

//

private void button1_Click( object sender , EventArgs e)

{

    CompilerParameters vCompilerParameters = new CompilerParameters ();

   vCompilerParameters . GenerateExecutable = false ;

   vCompilerParameters . GenerateInMemory = true ;

   vCompilerParameters . ReferencedAssemblies . Add(GetType() . Module . FullyQualifiedName);

   vCompilerParameters . ReferencedAssemblies . Add( "System.dll" );

   vCompilerParameters . ReferencedAssemblies . Add( "System.Windows.Forms.dll" );

    string vSource =

        "using " + GetType() . Namespace + ";\n" +

        "using System.ComponentModel;\n" +

        "using System.Windows.Forms;\n" +

        "public class Temp\n" +

        "{\n" +

        "    public void Test()\n" +

        "    {\n" +

        "         new " + GetType() . Name + "().Show();" +

        "    }\n" +

        "}\n" ;

    Console . WriteLine( typeof ( Component ) . Module . FullyQualifiedName);

    CompilerResults vCompilerResults = CodeDomProvider . CreateProvider(

        "CSharp" ) . CompileAssemblyFromSource(vCompilerParameters, vSource);

    if (vCompilerResults . Errors . Count > 0 )

   {

        foreach ( CompilerError vCompilerError in vCompilerResults . Errors)

       {

            Console . WriteLine( " + vCompilerError . ErrorText);

       }

        return ;

   }

    Assembly vAssembly = vCompilerResults . CompiledAssembly;

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

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

   vTest . Invoke(vTemp, null );

}