C#WPFと反射を用いる例.

6013 ワード

///WPF  button, TextBox, TextBlock



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





namespace WpfApplication2

{

    /// <summary>

    /// Window1.xaml  

    /// </summary>

    public partial class Window1 : Window

    {

        public Window1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, RoutedEventArgs e)

        {

            Class1 driver = new Class1();

            bool isHasError;

            textBlock1.Text = driver.CompileAndRun(textBox1.Text, out isHasError);

            if (isHasError)

            {

                textBlock1.Background = Brushes.Red;

            }



           



        }

    }

}



 



/// 。  , using 



 



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

//using WpfApplication2;

using System.CodeDom.Compiler;

using System.IO;

using Microsoft.CSharp;

using System.Reflection;





namespace ClassLibrary1

{

    public class Class1

    {

        private string prefix =

            "using System;" +

            "public static class Driver" +

            "{" +

            "public static void Run()" +

            "{";



        private string postfix = "}" +

        "}";





        public string CompileAndRun(string input, out bool isHasError)

        {

            isHasError = false;

            string returnData = null;



            CompilerResults results = null;

            using (CSharpCodeProvider provider = new CSharpCodeProvider())

            {

                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)

            {

                isHasError = true;

                StringBuilder errorMessage = new StringBuilder();

                foreach (CompilerError item in results.Errors)

                {



                    errorMessage.AppendFormat("{0} {1}", item.Line, item.ErrorText);



                }



                returnData = errorMessage.ToString();

            }

            else

            {

                TextWriter temp = Console.Out;

                StringWriter sw = new StringWriter();



                Console.SetOut(sw);

                Type driverType = results.CompiledAssembly.GetType("Driver");



  /// 。

              

  driverType.InvokeMember("Run", BindingFlags.InvokeMethod | BindingFlags.Static | 

BindingFlags.Public,null,null,null);



                Console.SetOut(temp);



                returnData = sw.ToString();





            }

            return returnData;

           



 

        }

    }

}



 



/////////



 。



 



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Reflection;



using Microsoft.CSharp;

using System.IO;

using System.CodeDom.Compiler;





namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            DoThis doThis = new DoThis();

            

        }

    }



    public class DoThis

    {

        Type myTestType = null;

        StringBuilder sb = new StringBuilder();

        string CodeStart = "using System;"+

            "public  static class MyTest" +

                         "{" +

                         "public static void Drive()" +

                         "{"+

                         "Console.WriteLine("+"\""+" Drive "+"\""+");";



        string CodeEnd = "}" +

            "}";



        public DoThis()

        {

            sb.Append(CodeStart);

            sb.Append(CodeEnd);

            CompilerResults results = null;

            using (CSharpCodeProvider csProv = new CSharpCodeProvider())

            {

                CompilerParameters options = new CompilerParameters();

                options.GenerateInMemory = true;



                //results = csProv.CompileAssemblyFromSource(options, sb.ToString());

                results = csProv.CompileAssemblyFromSource(options, sb.ToString());

                //Console.WriteLine(results.ToString());





 

            }



            if (results.Errors.HasErrors)

            {

                Console.WriteLine(" ");

                foreach (CompilerError item in results.Errors)

                {

                    Console.WriteLine(item.ErrorText.ToString()+item.Line.ToString());

                }



            }

            else

            {

                TextWriter temp = Console.Out;

                StringWriter sw = new StringWriter();

                Console.SetOut(sw);

               

                Console.WriteLine(results.CompiledAssembly.GetType("MyTest").ToString());

                myTestType=results.CompiledAssembly.GetType("MyTest");



                myTestType.InvokeMember("Drive", BindingFlags.InvokeMethod | BindingFlags.Static | 

BindingFlags.Public, null, null, null);

                Console.SetOut(temp);

                Console.WriteLine(sw.ToString());



         

            }





        }

        

       



    }

}