C # dynamic compilation and execution

Source: Internet
Author: User

Compile and execute code at runtime. As shown below.

 

Open button to Open any text file and load it to TextBox. It can be changed after loading.

Compile and execute the Compile button. The Code is as follows.

 

 

Code

 1 using System;
2 using System. Windows. Forms;
3 using System. IO;
4 using System. CodeDom. Compiler;
5 using Microsoft. CSharp;
6 using System. Reflection;
7
8 namespace DynamicCodeCompiler
9 {
10 public partial class Form1: Form
11 {
12 string dynammicCode;
13
14 public Form1 ()
15 {
16 InitializeComponent ();
17}
18
19 // open the file and load the code
20 private void button#click (object sender, EventArgs e)
21 {
22 OpenFileDialog ofd = new OpenFileDialog ();
23 ofd. Multiselect = false;
24
25 if (ofd. ShowDialog () = DialogResult. OK)
26 {
27 FileStream fs = new FileStream (ofd. FileName, FileMode. Open );
28 StreamReader sr = new StreamReader (fs );
29 dynammicCode = sr. ReadToEnd ();
30 textBox1.Text = dynammicCode;
31}
32}
33
34 private void button2_Click (object sender, EventArgs e)
35 {
36 if (textBox1.Text! = Null)
37 {
38 // 1. CSharpCodePrivoder
39 CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider ();
40
41 // 2. ICodeComplier
42 ICodeCompiler objICodeCompiler = objCSharpCodePrivoder. CreateCompiler ();
43
44 // 3. CompilerParameters
45 CompilerParameters objCompilerParameters = new CompilerParameters ();
46
47 // Add reference here. The code used must be referenced here or the compilation error may occur.
48 objCompilerParameters. ReferencedAssemblies. Add ("System. dll ");
49 objCompilerParameters. ReferencedAssemblies. Add ("System. Windows. Forms. dll ");
50 objCompilerParameters. GenerateExecutable = false;
51 objCompilerParameters. GenerateInMemory = true;
52
53 // 4. CompilerResults
54 CompilerResults cr = objICodeCompiler. CompileAssemblyFromSource (objCompilerParameters, textBox1.Text );
55
56 if (cr. Errors. HasErrors)
57 {
58 Console. WriteLine ("Compilation error :");
59 foreach (CompilerError err in cr. Errors)
60 {
61 Console. WriteLine (err. ErrorText );
62}
63}
64 else
65 {
66 // call the HelloWorld instance through reflection. The namespace and class are hard-coded as Test. Program.
67 Assembly objAssembly = cr. CompiledAssembly;
68 object objDynamicAssenmbly = objAssembly. CreateInstance ("Test. Program ");
69 MethodInfo objMI = objDynamicAssenmbly. GetType (). GetMethod ("Main ");
70
71 // Console. WriteLine (objMI. Invoke (objDynamicAssenmbly, null ));
72 objMI. Invoke (objDynamicAssenmbly, null );
73}
74
75}
76}
77}
78}
79

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.