CompilerParameters param = new CompilerParameters(references, outputfile, true);
param.ReferencedAssemblies.Add("test.dll");
<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"
mc:Ignorable="d"
Title="WorkFlowEditView" Height="450" Width="800"><Grid><StackPanel><Button x:Name="btn_Complie" Content="调试" Click="btn_Complie_Click"></Button><RichTextBox x:Name="RTX_Edit" Height="200"><FlowDocument><Paragraph><Run Text="
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
class MyClass
{
public string GetResult(){//只准改写此行之下(zjm试验室)
string str=System.Convert.ToString(7788);return999;}}
"/></Paragraph></FlowDocument></RichTextBox><RichTextBox x:Name="RTX_Message" Height="100"><FlowDocument><Paragraph><Run Text="信息打印平台:"/></Paragraph></FlowDocument></RichTextBox></StackPanel></Grid></Window>
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
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;
using System.Windows.Shapes;
namespace 工业视觉检测系统.Views
{/// <summary>/// WorkFlowEditView.xaml 的交互逻辑/// </summary>
public partial class WorkFlowEditView : Window
{
public WorkFlowEditView(){InitializeComponent();}/// <summary>/// 编译/// </summary>/// <param name="sender"></param>/// <param name="e"></param>
private voidbtn_Complie_Click(object sender, RoutedEventArgs e){
string text = new TextRange(RTX_Edit.Document.ContentStart, RTX_Edit.Document.ContentEnd).Text;
CodeCompiler.Compile(new string[]{}, 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>();/// <summary>/// 编译/// </summary>/// <param name="references"></param>/// <param name="source"></param>/// <param name="outputfile"></param>/// <returns></returns>
public static bool Compile(string[] references, string source, string outputfile){// 编译参数
CompilerParameters param = new CompilerParameters(references, outputfile, true);
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;}}}}