C# 一个用到WPF 和 反射的例子.

///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());

         
            }


        }
        
       

    }
}

  

原文地址:https://www.cnblogs.com/fat_li/p/1922343.html