zoukankan      html  css  js  c++  java
  • 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());
    
             
                }
    
    
            }
            
           
    
        }
    }
    

      

  • 相关阅读:
    操纵持久化对象
    面阵和线扫工业相机选型
    线扫描镜头简介及选型
    Halcon的anisotropic_diffusion()函数,用于对图像进行各向异性散射增强处理
    VB、C#等高级语言与三菱PLC(Q系列、L系列、FX系列、A系列)串口、以太网通讯的DLL及源代码
    Halcon学习笔记之支持向量机
    C#中使用byte[]数据,生成Bitmap(256色 灰度 BMP位图)源代码
    Halcon学习SVM
    利用MLP(多层感知器)创建一个新的OCR分级器
    Halcon中OCR的实现及关键函数解析
  • 原文地址:https://www.cnblogs.com/fat_li/p/1922343.html
Copyright © 2011-2022 走看看