C # dynamic compilation and reflection execution

Source: Internet
Author: User

# Static methods in region Assembly 1 with no parameters and static classes

Static void CodeAssembly_1 ()
{
// Provide access to instances of the C # code generator and code compiler.
CSharpCodeProvider provider = new CSharpCodeProvider ();

// Indicates the parameter used to call the compiler.
CompilerParameters parameter = new CompilerParameters ();
// Obtain the Assembly referenced by the current project.
Parameter. ReferencedAssemblies. Add ("System. dll ");
// Obtain or set a value indicating whether an executable file is generated.
Parameter. GenerateExecutable = false;
// Obtain or set a value that indicates whether output is generated in the memory.
Parameter. GenerateInMemory = true;

// Use the specified compiler settings to compile the Assembly from the specified array of strings containing the source code. Returns the compilation result.
CompilerResults result = provider. CompileAssemblyFromSource (parameter,
GetCode_1 ("120/12"); // put your formula here
// Obtain a set of Compiler Errors and warnings
If (result. Errors. Count> 0)
{
Console. WriteLine ("dynamic compilation error! ");
}
Else
{
// Obtain or set the compiled assembly.
Assembly assembly = result. CompiledAssembly;
// Obtain the Type of the current instance
Type AType = assembly. GetType ("NameSpace_1.Class_1 ");
// Method for obtaining the type
MethodInfo method = AType. GetMethod ("Func ");
// Call Method
Console. WriteLine (method. Invoke (null, null ));
}
Console. Read ();
}
// Get the code
Private static string GetCode_1 (string para)
{
StringBuilder codes = new StringBuilder ();
// Reference
Codes. Append ("using System ;");
// Namespace Definition
Codes. Append ("namespace NameSpace_1 ");
Codes. Append ("{");
// Class definition
Codes. Append ("public class Class_1 ");
Codes. Append ("{");
// Method definition
Codes. Append ("public static object Func ()");
Codes. Append ("{");
Codes. Append ("return (" + para + ");");
Codes. Append ("}");
Codes. Append ("}");
Codes. Append ("}");

Return (codes. ToString ());
}

# Endregion

 

 

 

# Region assembly 2 contains the constructor class. You need to generate an instance object and call the return parameter method.

Static void CodeAssembly_2 ()
{
// Provide access to instances of the C # code generator and code compiler.
CSharpCodeProvider provider = new CSharpCodeProvider ();

// Indicates the parameter used to call the compiler.
CompilerParameters parameter = new CompilerParameters ();
// Obtain the Assembly referenced by the current project.
Parameter. ReferencedAssemblies. Add ("System. dll ");
// Obtain or set a value indicating whether an executable file is generated.
Parameter. GenerateExecutable = false;
// Obtain or set a value that indicates whether output is generated in the memory.
Parameter. GenerateInMemory = true;

// Use the specified compiler settings to compile the Assembly from the specified array of strings containing the source code. Returns the compilation result.
CompilerResults result = provider. CompileAssemblyFromSource (parameter, GetCode_2 ());
// Obtain a set of Compiler Errors and warnings
& Nb

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.