当前位置:网站首页>自制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";
}
}
}
边栏推荐
猜你喜欢

What is EGFP, green fluorescent protein

China micro semiconductor has passed the registration: the annual revenue is 1.1 billion, and the actual controller is New Zealand nationality
![P6758 [BalticOI2013] Vim](/img/07/b16c8d329b33424e931d9eaedae73a.png)
P6758 [BalticOI2013] Vim

传承百年经典的瑞吉管家静待您的优雅旅程再次开启

Ln2220 2A overcurrent 5v1a High Efficiency Boost IC chip dc/dc voltage regulator

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

Hypebeast, un média à la mode, est sur le point d'être lancé: 530 millions de dollars pour le troisième trimestre

类似中国知网但是搜索英文文献的权威网站有哪些?

ACM. Hj61 put apple ●

潮流媒體Hypebeast擬曲線上市:作價5.3億美元 擬第三季完成
随机推荐
挖财赠送的证券账户安全吗?可以开户吗?
solidity实现智能合约教程(4)-ERC1155合约
12.信号基础
撰写学术论文引用文献时,标准的格式是怎样的?
传承百年经典的瑞吉管家静待您的优雅旅程再次开启
杰理之做蓝牙发射的时候,回链没有声音解决方法【篇】
Excuse me, which website is better for college students to check literature?
杰理之蓝牙发射器的搜索设备的时间修改方法【篇】
How to make file read only when an idea file is locked?
Thoroughly understand the foundation of MySQL: the difference between B tree and b+ tree
LeetCode508-出现次数最多的子树元素和-深搜
Qx2308 high efficiency PFM Synchronous Boost dc/dc converter
AWS CloudWatch
From casual to boring to sick
JS object operation (much simpler than C object)
英文论文要怎么查重?
杰理之,提示芯片信 息不匹配,错误代号 10 的处理方法【篇】
Eureka console accesses the info endpoint exposed by the microservice
Can I open an account for online stock trading? Is it safe
Six interesting web page effects that can be used