软件发布

手机版,更便捷!

下载排行榜首页软件下载安卓下载资讯教程推荐专题装机必备
当前位置:文章资讯 > 编程开发 >

怎么在动态执行页面输入代码?动态执行页面输入代码分享

时间:2016-12-31 浏览次数: 编辑:9upk

前台代码:TextBox实现换行  加上 TextWrapping="Wrap" AcceptsReturn="True" 两个属性就行了。

<Window x:Class="WpfAssembly.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="357" Width="582">
<Grid>
<Button Height="23" HorizontalAlignment="Right" Margin="0,134,8,0" Name="button1" VerticalAlignment="Top" Width="165" Click="button1_Click">执行</Button>
<TextBox Height="104" Margin="10,26,8,0" Name="textBox1" VerticalAlignment="Top" Foreground="Blue" BorderBrush="WhiteSmoke" Background="WhiteSmoke" TextWrapping="Wrap" AcceptsReturn="True" >
</TextBox>
<TextBlock Margin="10,0,8,8" Name="textBlock1" Background="NavajoWhite" Height="151" VerticalAlignment="Bottom" /><Label Height="27" HorizontalAlignment="Left" Margin="10,0,0,0" Name="label1" VerticalAlignment="Top" Width="120">代码输入</Label><Label HorizontalAlignment="Left" Margin="10,137,0,159" Name="label2" Width="120">代码输出</Label>
</Grid>
</Window>
 

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;

namespace WpfAssembly
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
CodeDriver driver = new CodeDriver();
bool isError;
textBlock1.Text = driver.ComplileAndRun(textBox1.Text,out isError);//调用执行代码的类
if (isError)
{
textBlock1.Background = Brushes.Red;
}
else
{
textBlock1.Background = Brushes.NavajoWhite;
}
}
}
public class CodeDriver
{
private string prefix = "using System;" +
"public static class Driver" +
"{" +
"public static void Run()" +
"{";
private string postfix = "}" + "}";
public string ComplileAndRun(string input, out bool hasError)
{
hasError = false;
string returnData = null;
CompilerResults results = null;
using (CSharpCodeProvider provider = new CSharpCodeProvider())//c#代码生成器和编译器的实例
{
CompilerParameters options = new CompilerParameters();
options.GenerateInMemory = true;//是否从内存输出
StringBuilder sb = new StringBuilder();
sb.Append(prefix);
sb.Append(input);
sb.Append(postfix);

results = provider.CompileAssemblyFromSource(options, sb.ToString());//向编译器输入代码字符

}
if (results.Errors.HasErrors)//代码运行是否正常
{
hasError = true;
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
sb.AppendFormat("{0} {1}", error.Line, error.ErrorText);
}
returnData = sb.ToString();
}
else
{
TextWriter tw = Console.Out;
StringWriter sw = new StringWriter();
Console.SetOut(sw);//接受输出的字符串
Type driverType = results.CompiledAssembly.GetType("Driver");//反射获取Driver类
//指定执行函数的参数列表
driverType.InvokeMember("Run", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, null);

Console.SetOut(tw);
returnData = sw.ToString();
}
return returnData;
}
}
}

标签:

上一篇:用汇编代码调用没有参数信息的dll库函下一篇:用Python实现Fibonacci函数的方法是什

相关文章

最新评论

本类排行榜

图文专题

  • 类地下城割草手游推荐
  • 种菜小游戏
  • 单机打鱼游戏
  • 好玩的放置修仙手游