demo
https://gitee.com/chenheze90/L21_Roslyn/repository/archive/master.zip
项目地址
https://gitee.com/chenheze90/L21_Roslyn
前言
Roslyn,是微软开发的C#和VB.NET的开源编译器。
最近一直在研究组态软件(scada),组态软件的价值极其客观,且功能非常强大,但是同时对技术的要求也非常高。其中一项技术——即时编译是无法绕过的一个技术点,本文记录这项技术的同时,也是一种分享。希望有共同兴趣的程序员一起加入,一起学习。
即时编译概念:即把文本编译成软件,甚至可以立刻在软件上使用起来。
Roslyn 支持 C# 和 VB.NET 两种编程语言。
1.创建一个wpf项目,界面及其代码如下
代码
<Window x:Class="RoslynDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RoslynDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<TextBox Name="txbCode" TextWrapping="Wrap" Height="390"></TextBox>
<Button Height="20" Width="120" Content="提交代码" Click="Button_Click"></Button>
</StackPanel>
</Window>
2.Roslyn需要在nuget上下载组件:Microsoft.CodeAnalysis.CSharp。如下图所示
后台代码如下,主要功能在Button_Click事件中。每个步骤加了注释,说明了代码的作用
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
namespace RoslynDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// 指定源代码。这里用的是直接中文本框中获取,当然也可以用其他的方式代替
SourceText st = SourceText.From(txbCode.Text);
// 指定源代码2。也可以直接用文本的方式
st = SourceText.From(@"public class TestRoslyn { public void Test(){ Console.FakeMethod(); } }");
// 确定使用C# 的版本和传入的预编译量
CSharpParseOptions option = new CSharpParseOptions(LanguageVersion.CSharp6, preprocessorSymbols: new List<string>() { "Debug" });
// 生成语法树。有做过反编译的工程师大多都知道语法树,这个是反编译的关键。
var tree = CSharpSyntaxTree.ParseText(st, option);
// debug
Debug.Assert(tree.GetDiagnostics().ToList().Count == 0);
// 声明编译选项
var compileOption = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
// 创建编译实例
var compilation = CSharpCompilation.Create(
"class1",
new List<SyntaxTree> { tree }, //这是前面生成的语法树
new List<MetadataReference>() { MetadataReference.CreateFromFile(typeof(int).Assembly.Location) },
compileOption);
Debug.Assert(tree.GetDiagnostics().ToList().Count == 0); //如果之前有引用缺失、调用方法错误,这里就会体现出来
var assemblyPath = @"class1.dll";
var pdbPath = @"class1.pdb";
// 编译输出文件
var res = compilation.Emit(assemblyPath, pdbPath); //result表明了执行是否成功
}
}
}
方法执行之后,生产程序文件。接下来就是通过反射,来调用其中的方法了。