您的位置:首页 > 编程语言

使用“代码文档对象模型”(CodeDOM) 生成源代码和可执行文件。

2007-03-22 14:43 811 查看

使用“代码文档对象模型”(CodeDOM) 生成源代码和可执行文件。
此示例生成的程序只是输出用惯了的"Hello World!",但是已经可以起到示范的作用了。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 此程序由jlgzw编写于 2007.3
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace ConsoleApplication1
{
class Program
{
private static string strCodeFileName = "Test.cs";

static void Main(string[] args)
{
CodeCompileUnit CompileUnit = new CodeCompileUnit();

CodeNamespace cn = new CodeNamespace("NS");

CompileUnit.Namespaces.Add(cn);

cn.Imports.Add(new CodeNamespaceImport("System"));

CodeTypeDeclaration Class1 = new CodeTypeDeclaration("Class1");

CodeConstructor constructor1 = new CodeConstructor();
constructor1.Attributes = MemberAttributes.Public;
Class1.Members.Add(constructor1);

CodeMemberMethod method1 = new CodeMemberMethod();
method1.Name = "ReturnString";
method1.Attributes = MemberAttributes.Private;
method1.ReturnType = new CodeTypeReference("System.String");
method1.Parameters.Add( new CodeParameterDeclarationExpression("System.String", "Text") );
method1.Statements.Add(new CodeMethodReturnStatement( new CodeArgumentReferenceExpression("Text")

) );

Class1.Members.Add(method1);

cn.Types.Add(Class1);

CodeEntryPointMethod Start = new CodeEntryPointMethod();

CodeVariableDeclarationStatement varDeclaration1 = new CodeVariableDeclarationStatement(
typeof(string),"str",new CodePrimitiveExpression("Hello World!"));

Start.Statements.Add(varDeclaration1);

CodeVariableDeclarationStatement varDeclaration2 = new CodeVariableDeclarationStatement(new

CodeTypeReference("Class1"), "class1",
new CodeObjectCreateExpression( "Class1", new CodeExpression[] {} ));

Start.Statements.Add(varDeclaration2);

CodeMethodInvokeExpression mb = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression

("System.Windows.Forms.MessageBox"),
"Show",
new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("class1"), "ReturnString",
new CodeVariableReferenceExpression("str")));

Start.Statements.Add(new CodeExpressionStatement(mb));

Class1.Members.Add(Start);

CodeDomProvider provider = new CSharpCodeProvider();

GenerateCode(provider, CompileUnit);

CompilerResults cr = CompileCode(provider, strCodeFileName);

if (cr.Errors.Count > 0)
{
Console.WriteLine("编译遭遇错误 " + cr.PathToAssembly + ": /r/n");
foreach (CompilerError ce in cr.Errors)
Console.WriteLine(ce.ToString() + "/r/n");
}
else
{
Console.WriteLine(cr.PathToAssembly + " 生成成功。");
}
}
public static void GenerateCode(CodeDomProvider provider, CodeCompileUnit compileunit)
{
IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(strCodeFileName, false), " ");
// Generate source code using the code generator.
provider.GenerateCodeFromCompileUnit(compileunit, tw, new CodeGeneratorOptions());
// Close the output file.
tw.Close();
}
public static CompilerResults CompileCode(CodeDomProvider provider, string filepath)
{
CompilerParameters cp = new CompilerParameters(new string[] { "System.dll"

,"System.Windows.Forms.dll"}, filepath.Substring(0, filepath.LastIndexOf(".") + 1) + "exe", false);

cp.GenerateExecutable = true;

CompilerResults cr = provider.CompileAssemblyFromFile(cp, filepath);

return cr;
}
}
}

下面是生成的Test.cs:

//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行库版本:2.0.50727.42
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace NS {
using System;

public class Class1 {

public Class1() {
}

private string ReturnString(string Text) {
return Text;
}

public static void Main() {
string str = "Hello World!";
Class1 class1 = new Class1();
System.Windows.Forms.MessageBox.Show(class1.ReturnString(str));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐