西西软件园多重安全检测下载网站、值得信赖的软件下载站!
软件
软件
文章
搜索

首页编程开发C#.NET → 动态执行页面输入代码

动态执行页面输入代码

相关软件相关文章发表评论 来源:本站整理时间:2010/8/30 22:05:54字体大小:A-A+

作者:佚名点击:198次评论:0次标签: TextBox

  • 类型:动态模板大小:2.8M语言:中文 评分:10.0
  • 标签:
立即下载

前台代码: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;
}
}
}

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)