使用“代码文档对象模型”(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));
}
}
}
相关文章推荐
- 你真的了解jQuery的ready函数吗?当 DOM(文档对象模型) 已经加载,就执行ready里的代码。DOM何时加载完毕?jQuery对html局部操作,DOM为什么不重新加载?
- Python - 使用Pyinstaller将Python代码生成可执行文件
- Android -使用DOM(文档对象模型)解析XML文件
- Android -使用DOM(文档对象模型)解析XML文件
- smarty模板文件中执行php代码 调用php函数的方法 使用对象等
- Android -使用DOM(文档对象模型)解析XML文件
- CodeDom 系列:代码文档对象模型
- 在运行程序时报错:"如果在 Code First 模式下使用,则使用 T4 模板为 Database First 和 Model First 开发生成的代码可能无法 正常运行。若要继续使用 Database First 或 Model First,请确保在执行应用程序的 config 文件中指 定 Entity Framework 连接字符串。若要将这些从 Database First 或 Mod
- 使用PDFLib生成PDF文档,C代码实例--画图形
- Xcode8 使用CoreData生成对象文件出现编译错误解决方案
- Swift编码规范之注释规范:文件注释、文档注释、代码注释、使用地标注释
- 在邮件合并时使用Aspose.Words生成多个文档的具体代码
- 代码笔记:使用Xml2Linq和CodeDom自动重整多语化资源文件
- 使用doxygen从c++代码中生成文档的方法学习 .
- Android学习笔记_31_通过后台代码生成View对象以及动态加载XML布局文件到LinearLayout
- 《从零開始学Swift》学习笔记(Day 57)——Swift编码规范之凝视规范:文件凝视、文档凝视、代码凝视、使用地标凝视
- [设计模式]在CodeDom代码生成中使用Decorator模式实现类型创建
- 《从零开始学Swift》学习笔记(Day 57)——Swift编码规范之注释规范:文件注释、文档注释、代码注释、使用地标注释
- cocos2dx 代码使用cocoStudio 生成的场景json文件
- log4j2 不使用配置文件,动态生成logger对象