(go + organize) dynamic code execution in C #

Source: Internet
Author: User

Tag: Message code executes itself enc RPC ESS generic OBJC. dll

The Csharpcodeprovider,compilerparameters,compilerresults class, which is provided by Microsoft, allows you to dynamically execute code files that you write at run time. The principle is to dynamically compile your code files into an EXE or DLL, or output in memory, then reflect through assembly, execute methods in them, or access properties in them.

The interface has two buttons, one executes the ShowMessage method, the other executes the ShowForm method, the code is as follows:

Need to introduce

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using Microsoft.csharp;
Using System.CodeDom;
Using System.CodeDom.Compiler;
Using System.Reflection;

Namespace Codetest
{
public partial class Test:form
{
Public Test ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{
CSharpCodeProvider Objcsharpcodeprivoder = new CSharpCodeProvider ();
CompilerParameters objcompilerparameters = new CompilerParameters ();

OBJCOMPILERPARAMETERS.REFERENCEDASSEMBLIES.ADD ("System.dll");
OBJCOMPILERPARAMETERS.REFERENCEDASSEMBLIES.ADD ("System.Windows.Forms.dll");

Objcompilerparameters.generateexecutable = false;
Objcompilerparameters.generateinmemory = true;

CompilerResults Cresult = Objcsharpcodeprivoder.compileassemblyfromsource (objcompilerparameters, TextBox1.Text);

if (Cresult. Errors.haserrors)
{
foreach (CompilerError err in Cresult. Errors)
{
MessageBox.Show (Err. ErrorText);
}
}
Else
{
Execute the code through reflection
Assembly objassembly = cresult.compiledassembly;
Object obj = objassembly.createinstance ("Codetest.test");
MethodInfo Objmi = obj. GetType (). GetMethod ("ShowMessage");
Objmi.invoke (obj, new object[] {"This is codetest!"});
}
}

private void Form1_Load (object sender, EventArgs e)
{
TextBox1.Text = "Using System;" + Environment.NewLine +
"Using System.Windows.Forms;" + Environment.NewLine +
Environment.NewLine +
"Namespace Codetest" + Environment.NewLine +
"{" + Environment.NewLine +
"Public partial class Test" + Environment.NewLine +
"{" + Environment.NewLine +
' \ t ' + ' public void ShowMessage (string msg) ' + Environment.NewLine +
' \ t ' + ' {' + Environment.NewLine +
' \ t ' + ' MessageBox.Show (msg); "+ Environment.NewLine +
' \ t ' + '} ' + Environment.NewLine +
Environment.NewLine +
' \ t ' + ' public void showform () ' + Environment.NewLine +
' \ t ' + ' {' + Environment.NewLine +
' \ t ' + ' form frm = new form (); "+ Environment.NewLine +
' \ t ' + ' frm. Show (); "+ Environment.NewLine +
' \ t ' + '} ' + Environment.NewLine +
"}" + Environment.NewLine +
"}";
}

private void Button2_Click (object sender, EventArgs e)
{

CSharpCodeProvider Objcsharpcodeprivoder = new CSharpCodeProvider ();
CompilerParameters objcompilerparameters = new CompilerParameters ();

OBJCOMPILERPARAMETERS.REFERENCEDASSEMBLIES.ADD ("System.dll");
OBJCOMPILERPARAMETERS.REFERENCEDASSEMBLIES.ADD ("System.Windows.Forms.dll");

Objcompilerparameters.generateexecutable = false;
Objcompilerparameters.generateinmemory = true;

CompilerResults Cresult = Objcsharpcodeprivoder.compileassemblyfromsource (objcompilerparameters, TextBox1.Text);

if (Cresult. Errors.haserrors)
{
foreach (CompilerError err in Cresult. Errors)
{
MessageBox.Show (Err. ErrorText);
}
}
Else
{
Execute the code through reflection
Assembly objassembly = cresult.compiledassembly;
Object obj = objassembly.createinstance ("Codetest.test");
MethodInfo Objmi = obj. GetType (). GetMethod ("ShowForm");
Objmi.invoke (obj, null);
}
}
}
}

Click the button "execute code" to run the following results:

Click the button "Show form" and the code executes as follows:

Because the custom code has

Using System;
Using System.Windows.Forms;

The above two class libraries are required to execute, so to add a reference to these two DLLs in CompilerParameters,

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

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

When reflecting, the name of the namespace is written in full, the namespace in the instance is Codetest, the class is Test, so the reflection code is objassembly.createinstance ("Codetest.test")

Article reprinted from 22398077

(go + organize) dynamic code execution in C #

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.