使用“代码文档对象模型”(CodeDOM) 生成源代码和可执行文件。
来源:互联网 发布:arduino图形化编程软件 编辑:程序博客网 时间:2024/05/17 04:58
使用“代码文档对象模型”(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));
}
}
}
- 使用“代码文档对象模型”(CodeDOM) 生成源代码和可执行文件。
- CodeDom 系列:代码文档对象模型
- 使用CODEDOM动态实现代码的生成,编译
- 使用CodeDom生成程序集
- 使用CodeDom生成程序集
- 使用CodeDom生成程序集
- 使用CodeDom执行动态代码
- 使用 .Net Framework 的 System.CodeDom 命名空间动态生成源代码并编译
- 使用CodeDom生成程序集(在你的代码中用程序生成一个程序)
- C#动态生成代码——CodeDom
- 使用CodeDom来动态生成.cs文件
- C++源代码如何生成可执行文件
- CodeDom使用
- 使用py2exe生成可执行文件
- 使用Doxygen生成代码文档
- 使用Doxygen生成代码文档
- C++源代码生成可执行文件的四个步骤
- 怎样用Eclipse将Java源代码生成可执行文件
- 在2005中使用DrillThrough函数
- 关于在Java程序当中创建对象等基本操作的性能测试
- 价格数字正则表达式验证
- 在C#中利用Excel做高级报表
- sqlserver 中的日期转换
- 使用“代码文档对象模型”(CodeDOM) 生成源代码和可执行文件。
- j2me入门-symbian os support for MIDP
- linux2.6内核epoll用法举例说明
- C#3.0都出来了!
- SQL查询重复记录
- Linux - rc.d
- Web应用中的中文问题
- 详解JAVA语言中的访问控制
- 浪漫情书