西西軟件園多重安全檢測下載網站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)C#.NET → 動態(tài)執(zhí)行頁面輸入代碼

動態(tài)執(zhí)行頁面輸入代碼

相關軟件相關文章發(fā)表評論 來源:本站整理時間:2010/8/30 22:05:54字體大。A-A+

作者:佚名點擊:198次評論:0次標簽: TextBox

  • 類型:動態(tài)模板大。2.8M語言:中文 評分:10.0
  • 標簽:
立即下載

前臺代碼:TextBox實現(xiàn)換行  加上 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">執(zhí)行</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);//調用執(zhí)行代碼的類
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類
//指定執(zhí)行函數(shù)的參數(shù)列表
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 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字數(shù): 0/500 (您的評論需要經過審核才能顯示)