中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > C#
C#编译器
作者:未知 时间:2005-08-07 21:12 出处:编程爱好者网站 责编:chinaitpower
              摘要:C#编译器
以下是国外人写的一个C#编译器,当然由于他依然使用的.Net下的FCL,所以不可能是真正意义上的编译器, 飞刀我介绍出来,只是可以让大家摆脱csc的约束,在Win界面上完成编译.


你必须以下面的步骤完成:

1.建立一个CSharpCodeProvider 实例(如果是使用Visual Basic则使用VBCodeProvider)
2.包含接口ICodeCompiler
3.提供CompilerParameters的参数
4.使用CompileAssemblyFromSource方法编译。
5.运行CompilerResults
6.执行编译好的程序


编译的代码可以是写在文本框中的字符串,当然也可以源文件。

以下是源代码:),过两天我修改一下后提供整个源代码:)

private void button1_Click(object sender, System.EventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
// For Visual Basic Compiler try this :
//Microsoft.VisualBasic.VBCodeProvider

ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters();

parameters.GenerateExecutable = true;
if (appName.Text == "")
{
System.Windows.Forms.MessageBox.Show(this,
"Application name cannot be empty");
return ;
}

parameters.OutputAssembly = appName.Text.ToString();

if (mainClass.Text.ToString() == "")
{
System.Windows.Forms.MessageBox.Show(this,
"Main Class Name cannot be empty");
return ;
}

parameters.MainClass = mainClass.Text.ToString();
parameters.IncludeDebugInformation = includeDebug.Checked;

// Add available assemblies - this should be enough for the simplest
// applications.
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
parameters.ReferencedAssemblies.Add(asm.Location);
}

String code = textBox1.Text.ToString();
//System.Windows.Forms.MessageBox.Show(this, code);

CompilerResults results = compiler.CompileAssemblyFromSource(parameters,
code);

if (results.Errors.Count > 0)
{
string errors = "Compilation failed:\n";
foreach (CompilerError err in results.Errors)
{
errors += err.ToString() + "\n";
}
System.Windows.Forms.MessageBox.Show(this, errors,
"There were compilation errors");
}
else
{
#region Executing generated executable
// try to execute application
try
{
if (!System.IO.File.Exists(appName.Text.ToString()))
{
MessageBox.Show(String.Format("Can't find {0}", appName),
"Can't execute.", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
ProcessStartInfo pInfo = new ProcessStartInfo(appName.Text.ToString());
Process.Start(pInfo);
}
catch (Exception ex)
{
MessageBox.Show(String.Format("Error while executing {0}",
appName) + ex.ToString(), "Can't execute.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

#endregion

}

}

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有