# 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