当前位置:网站首页>自制C#编译器
自制C#编译器
2022-06-21 20:13:00 【金增辉】
源代码下载链接:
添加链接描述
这里是引用
运行结果:

人狠话不多,直接上代码:
using CodeEditor.CodeEdit;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CodeEditor
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnCompile_Click(object sender, EventArgs e)
{
//【1】新建C#代码生成器和代码编译器的实例
CodeDomProvider Provider = CodeDomProvider.CreateProvider("CSharp");
//【2】配置用于调用编译器的参数
CompilerParameters Parameters = new CompilerParameters();
Parameters.ReferencedAssemblies.Add("System.dll");
Parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
Parameters.ReferencedAssemblies.Add("System.Linq.dll");
Parameters.GenerateExecutable = false;
Parameters.GenerateInMemory = true;
//【3】启动编译
CompilerResults Result = Provider.CompileAssemblyFromSource(Parameters, rtbCode.Text);
if (Result.Errors.HasErrors)
{
AppendInfo("编译错误:");
foreach (CompilerError err in Result.Errors)
{
AppendInfo(err.ErrorText);
}
}
else
{
// 通过反射,调用实例
Assembly objAssembly = Result.CompiledAssembly;
object objHelloWorld = objAssembly.CreateInstance("CodeEditor.CodeEdit.Code");
MethodInfo objMI = objHelloWorld.GetType().GetMethod("Test");
object ReValue = objMI.Invoke(objHelloWorld, null);
AppendInfo(ReValue);
}
}
//追加字符
private void AppendInfo(object Info)
{
rtbResult.Text =Info+"\n\r";
}
}
}
边栏推荐
- Worthington胰蛋白酶解决方案
- 电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
- What are the authoritative websites that search English documents like CNKI?
- Build Eureka server cluster
- How to use the free and easy-to-use reference management software Zotero? Can I support both Chinese and English
- 英文论文的proposal怎么写?
- Eureka console accesses the info endpoint exposed by the microservice
- 英文论文如何进行润色?
- 阿里云容器服务负责人易立:云原生如何解企业降本提效难题?
- ACM. Hj51 outputs the penultimate node ●
猜你喜欢
![Jerry wants to keep both earphones output stereo after pairing them [chapter]](/img/47/c3b09561e87a58226fa232e30747d7.png)
Jerry wants to keep both earphones output stereo after pairing them [chapter]

phpmailer 通过smtp发送邮件,相同发送内容有的成功有的失败

#16迭代器经典案例

小程序怎样关联微信小程序二维码,实现二码合一聚合

Enzo高灵敏度检测——Arg8-Vasopressin ELISA kit

电子招标采购商城系统:优化传统采购业务,提速企业数字化升级

Tx9116 Synchronous Boost IC
![There is no sound solution to the loopback when jerryzhi launches Bluetooth [chapter]](/img/ba/377ec19ca22c2c106f227e864f1e9e.png)
There is no sound solution to the loopback when jerryzhi launches Bluetooth [chapter]

ACM. Hj61 put apple ●

China micro semiconductor has passed the registration: the annual revenue is 1.1 billion, and the actual controller is New Zealand nationality
随机推荐
Go单元测试对数据库CRUD进行Mock测试
Excuse me, which website is better for college students to check literature?
类似中国知网但是搜索英文文献的权威网站有哪些?
Prototype extension: implementing object inheritance
15 iterator
六个拿来就能用的有趣网页特效
How to search foreign literature efficiently?
In the anchoring stage of retail digitalization, more attention is paid to how to mine and transform traffic by means of Digitalization
Summary of internship interview experience of more than ten failed internship positions
InteliJ-IDEA-高效技巧(一)
Revenue and profit "ebb and flow", water drops turn in pain
先进封装,一个大周期的开始——“迎风国潮”半导体设备研讨会
CF1481F AB Tree
China micro semiconductor has passed the registration: the annual revenue is 1.1 billion, and the actual controller is New Zealand nationality
Jerizhi, processing method for prompting chip information mismatch and error code 10 [chapter]
天齐锂业通过聆讯:单季净利33亿 蒋卫平夫妇身价超500亿
英文论文的proposal怎么写?
免费又好用的参考文献管理软件zotero该怎么使用?可以同时支持中文和英文吗?...
Go unit test mocks the database CRUD
同样是做自媒体,为什么有的人能月入过万,你只能月入几块?