Use CodeDom to generate the. cs File

Source: Internet
Author: User

In learning how to use CodeDom to dynamically generate a. cs file, the Code fails to be compiled using the help example.
Modify and debug by yourself. The main code is as follows:

Namespace:
Using System. CodeDom;
Using System. CodeDom. Compiler;
Using Microsoft. CSharp;
Using System. IO;

Private void button#click (object sender, System. EventArgs e)
{
CodeCompileUnit CompileUnit = new CodeCompileUnit ();
CodeNamespace Samples = new CodeNamespace ("Samples ");
Samples. Imports. Add (new CodeNamespaceImport ("System "));
CompileUnit. Namespaces. Add (Samples );
CodeTypeDeclaration Class1 = new CodeTypeDeclaration ("Class1 ");
Samples. Types. Add (Class1 );

CodeEntryPointMethod Start = new CodeEntryPointMethod ();

// Output HelloWord
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression (new
CodeTypeReferenceExpression ("System. Console"), "WriteLine", new
CodePrimitiveExpression ("Hello World! "));

Start. Statements. Add (cs1 );

Class1.Members. Add (Start );
// CSharpCodeProvider provider = new CSharpCodeProvider ();
// ICodeGenerator gen = provider. CreateGenerator ();
GenerateGraph (CompileUnit );

}
Public void GenerateGraph (CodeCompileUnit compileunit)
{
// Obtains an ICodeGenerator from a CodeDomProvider class.
CSharpCodeProvider provider = new CSharpCodeProvider ();
ICodeGenerator gen = provider. CreateGenerator ();

// Creates a StreamWriter to an output file.
StreamWriter sw = new StreamWriter ("d: \ TestGraph. cs", false );

// Generates source code using the code generator.
Gen. GenerateCodeFromCompileUnit (compileunit, sw, new CodeGeneratorOptions ());

// Closes the output files.
Sw. Close ();
}

Private void button2_Click (object sender, System. EventArgs e)
{
CompileCode ("d: \ TestGraph. cs ");
}
// Edit and generate the Exe
Public CompilerResults CompileCode (string filepath)
{
// Obtains an ICodeCompiler from a CodeDomProvider class.
CSharpCodeProvider provider = new CSharpCodeProvider ();
ICodeCompiler compiler = provider. CreateCompiler ();

// Configures a compiler parameters object which links System. dll and
// Generates a file name based on the specified source file name.
CompilerParameters cp = new CompilerParameters (new string [] {"System. dll "}, filepath. substring (0, filepath. lastIndexOf (". ") + 1) +" exe ", false );

// Indicates that an executable rather than a. dll shocould be generated.
Cp. GenerateExecutable = true;

// Invokes compilation.
CompilerResults cr = compiler. CompileAssemblyFromFile (cp, filepath );

// Returns the results of compilation.
Return cr;
}

Examples in help are as follows:
. NET Framework-> program using. NET Framework-> dynamically generate and compile source code in multiple languages

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.