有关反射的一些理解

来源:互联网 发布:大数据运维管理 编辑:程序博客网 时间:2024/04/28 01:13
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace xmlTojson
{
    public partial class DynamicForm : Form
    {
        public DynamicForm()
        {
            InitializeComponent();
        }




        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.CheckFileExists = true;
                dlg.Filter = "程序集|*.exe|程序集|*.dll|所有文件|*.*";
                //dlg.Filter = "程序集|*.exe;*.dll|所有文件|*.*";
                if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    //Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    string friendlyname = AppDomain.CurrentDomain.FriendlyName;
                    //friendlyname = Application.StartupPath;
                    AppDomainSetup setss = new AppDomainSetup();
                    setss.ApplicationBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Bin");
                    setss.ShadowCopyFiles = "true";
                    setss.ShadowCopyDirectories = setss.ApplicationBase;
                    AppDomain appdomain = AppDomain.CreateDomain("aaa", null, setss);
                    appdomain.ExecuteAssembly(@"Bin\DCSoft.WinFormDemo.exe");
                    File.Delete(@"Bin\DCSoft.WinFormDemo.exe");




                    Assembly assembly = Assembly.LoadFrom(dlg.FileName);
                    if (assembly != null)
                    {
                        Type typeClass = null;
                        Type[] types = assembly.GetTypes();
                        foreach (Type type in types)
                        {
                            if (type.Name == "类名")
                            {
                                typeClass = type;
                                break;
                            }
                        }
                        #region 获取类型中的构造函数
                        //获取类型中的构造函数
                        ConstructorInfo[] Constructors = typeClass.GetConstructors();
                        //无参构造函数  初始化
                        object sss = Constructors[0].Invoke(new object[] { });
                        ////有参构造函数  初始化
                        ////object sss1 = Constructors[0].Invoke(new object[] { 1, 2, 2 });
                        ////另外一种方式   初始化
                        //object sss2 = assembly.CreateInstance("namespace.class");
                        ////另外一种方式---最佳方式   初始化
                        //object sss3 = Activator.CreateInstance(typeClass, new object[] {1,2,3 }); 
                        #endregion




                        //获取方法的自定义特性
                        Attribute assemblyattribute = Attribute.GetCustomAttribute(typeClass, typeof(xmlTojson.MyTestProperty), true);
                        //Attribute[] attributes = Attribute.GetCustomAttributes(typeClass, typeof(xmlTojson.MyTestProperty), true);




                        #region 获取类型中的方法
                        //获取类型中的方法
                        MethodInfo[] methods = typeClass.GetMethods(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public);
                        foreach (MethodInfo method in methods)
                        {
                            //获取方法的自定义特性
                            Attribute attribute = Attribute.GetCustomAttribute(method, typeof(xmlTojson.MyTestProperty), true);
                            //Attribute[] attributes = Attribute.GetCustomAttributes(method, typeof(xmlTojson.MyTestProperty), true);
                            //方法的调用
                            method.Invoke(sss, new object[] { 1, 4 });
                        }
                        #endregion




                        #region 获取类型中的属性
                        //获取类型中的属性
                        PropertyInfo[] properties = typeClass.GetProperties();
                        foreach (PropertyInfo property in properties)
                        {
                            //property.SetValue()
                            Attribute[] propertyattribute = (Attribute[])property.GetCustomAttributes(true);
                            //获取方法的自定义特性
                            Attribute attribute = Attribute.GetCustomAttribute(property, typeof(xmlTojson.MyTestProperty), true);
                            //Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
                        }
                        #endregion




                        #region 获取类型中的字段
                        //获取类型中的字段
                        FieldInfo[] fields = typeClass.GetFields();
                        foreach (FieldInfo field in fields)
                        {
                            Attribute[] propertyattribute = (Attribute[])field.GetCustomAttributes(true);
                            //获取方法的自定义特性
                            Attribute attribute = Attribute.GetCustomAttribute(field, typeof(xmlTojson.MyTestProperty), true);
                            //Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
                        
                            field.SetValue("字段名","值");
                        }
                        #endregion


                        #region 获取类型中的事件
                        //获取类型中的事件
                        EventInfo[] events = typeClass.GetEvents();
                        foreach (EventInfo @event in events)
                        {
                            Attribute[] propertyattribute = (Attribute[])@event.GetCustomAttributes(true);
                            //获取方法的自定义特性
                            Attribute attribute = Attribute.GetCustomAttribute(@event, typeof(xmlTojson.MyTestProperty), true);
                            //Attribute[] attributes = Attribute.GetCustomAttributes(property, typeof(xmlTojson.MyTestProperty), true);
                            //给事件新增程序的处理方法
                            @event.AddEventHandler(new Control(), Delegate.CreateDelegate(@event.EventHandlerType, typeClass.GetMethod("方法名")));
                        }
                        #endregion
                    }
                    this.richTextBox1.Text = dlg.FileName;
                }
            }
        }
    }
}
0 0
原创粉丝点击