WPF使用avalonEdit进行C#代码编辑
<Window x:Class="工业视觉检测系统.Views.WorkFlowEditView"
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:工业视觉检测系统.Views"
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
mc:Ignorable="d"
Title="WorkFlowEditView" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="7*" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<!--编译调试-->
<Button Grid.Row="0" x:Name="btn_Complie" Content="编译调试" Click="btn_Complie_Click" ></Button>
<!--代码编辑-->
<avalonEdit:TextEditor
Grid.Row="1"
Name="CodeEditor"
SyntaxHighlighting="C#"
FontFamily="Consolas"
FontSize="10pt"
LineNumbersForeground="Black"
ShowLineNumbers="True">
</avalonEdit:TextEditor>
<!--信息显示-->
<RichTextBox Grid.Row="2" x:Name="RTX_Message" Height="100" >
<FlowDocument>
<Paragraph>
<Run Text="信息打印平台:"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Window>
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace 工业视觉检测系统.Views
{
public partial class WorkFlowEditView : Window
{
public WorkFlowEditView()
{
InitializeComponent();
}
private void btn_Complie_Click(object sender, RoutedEventArgs e)
{
string text = CodeEditor.Document.Text;
string[] allDlls = Directory.GetFiles("./").Where(item=>Path.GetExtension(item).Equals(".dll")).ToArray();
CodeCompiler.Compile(allDlls , text, "");
foreach (string s in CodeCompiler.ErrorMessage)
{
RTX_Message.Dispatcher.Invoke(new Action(() => { RTX_Message.AppendText(s + "\r\n"); }));
}
RTX_Message.Dispatcher.Invoke(new Action(() => { RTX_Message.AppendText(CodeCompiler.Message + "\r\n"); }));
}
static class CodeCompiler
{
static public string Message;
static public List<string> ErrorMessage = new List<string>();
public static bool Compile(string[] references, string source, string outputfile)
{
CompilerParameters param = new CompilerParameters(references, outputfile, true);
param.ReferencedAssemblies.Add(@"工业视觉检测系统.exe");
param.TreatWarningsAsErrors = false;
param.GenerateExecutable = false;
param.IncludeDebugInformation = true;
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerResults result = provider.CompileAssemblyFromSource(param, new string[] { source });
Message = "";
ErrorMessage.Clear();
if (!result.Errors.HasErrors)
{
Type t = result.CompiledAssembly.GetType("MyClass");
if (t != null)
{
object o = result.CompiledAssembly.CreateInstance("MyClass");
Message = (string)t.InvokeMember("GetResult", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, o, null);
}
return true;
}
foreach (CompilerError error in result.Errors)
{
if (error.IsWarning) continue;
ErrorMessage.Add("Error(" + error.ErrorNumber + ") - " + error.ErrorText + "\t\tLine:" + error.Line.ToString() + " Column:" + error.Column.ToString());
}
return false;
}
}
}
}