日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
      軟件
      軟件
      文章
      搜索

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

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

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

      作者:佚名點(diǎn)擊:198次評(píng)論:0次標(biāo)簽: TextBox

      • 類型:動(dòng)態(tài)模板大。2.8M語言:中文 評(píng)分:10.0
      • 標(biāo)簽:
      立即下載

      前臺(tái)代碼:TextBox實(shí)現(xiàn)換行  加上 TextWrapping="Wrap" AcceptsReturn="True" 兩個(gè)屬性就行了。

      <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>
       

      后臺(tái)代碼:

      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);//調(diào)用執(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#代碼生成器和編譯器的實(shí)例
      {
      CompilerParameters options = new CompilerParameters();
      options.GenerateInMemory = true;//是否從內(nèi)存輸出
      StringBuilder sb = new StringBuilder();
      sb.Append(prefix);
      sb.Append(input);
      sb.Append(postfix);

      results = provider.CompileAssemblyFromSource(options, sb.ToString());//向編譯器輸入代碼字符

      }
      if (results.Errors.HasErrors)//代碼運(yùn)行是否正常
      {
      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;
      }
      }
      }

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評(píng)論

        最新評(píng)論

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

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