C#-Using reflection to create an instance

The concept of reflection: the process of dynamically loading an assembly by obtaining its type and creating an object to call its members is called reflection (provided there is an assembly)

 //动态加载程序集
            Assembly Asm = Assembly.LoadFile(@"C:\Users\俗子。\Desktop\ADO.NET练习\反射01\01Animal\bin\Debug\01Animal.exe");
            //获取类型
            Type[] type = Asm.GetTypes();//获取所有类型
            Asm.GetExportedTypes();//获取多有公共类型
            Type Person = Asm.GetType("_01Animal.person");//获取指定的类型
            //获取方法,如果获取重载,则用第二个参数TYPE[]数组表示即可
            MethodInfo info = Person.GetMethod("SayHi");//获取指定方法
                                                        //创建对象
                                                        //object c = Activator.CreateInstance(Person);//无参构造方法创建对象
            ConstructorInfo Cons = Person.GetConstructor(new Type[] {
    
     typeof(string), typeof(string), typeof(int) });//使用type数组来表示参数
            object c = Cons.Invoke(new object[] {
    
     "张三", "女", 25 });//对象
            //执行
            object str = info.Invoke(c, null);//第一个参数是实例
            MessageBox.Show(str.ToString());//成功!
            //获取指定属性
            PropertyInfo A = Person.GetProperty("Name");
            //MessageBox.Show(A.Name);//获取属性的名称
            MessageBox.Show(A.GetValue(c, null).ToString());//获取属性值
            PropertyInfo B = Person.GetProperty("Sex");
            // MessageBox.Show(B.Name);//获取性别的名称
            MessageBox.Show(B.GetValue(c, null).ToString());//获取属性值
            PropertyInfo C = Person.GetProperty("Age");
            // MessageBox.Show(C.Name.ToString());//获取年龄属性名称
            MessageBox.Show(C.GetValue(c, null).ToString());

Several types of instances are created:

object c = Activator.CreateInstance(Person);//无参构造方法创建对象

//参数化构造函数
ConstructorInfo Cons = Person.GetConstructor(new Type[] {
    
     typeof(string), typeof(string), typeof(int) });//使用type数组来表示参数
object c = Cons.Invoke(new object[] {
    
     "张三", "女", 25 });//对象

//加载程序集并创建指定类型实例
 IUserInfoDal userinfo=Assembly.Load("MyProject_OA_EFDal").CreateInstance("MyProject_OA_EFDal.UserInFoDal") as IUserInfoDal;


The notebook provides extensions:

             #region 03
            //先获取程序集目录路径
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Addons");
            string[] interpath = Directory.GetFiles(path, "*.dll");
            for (int i = 0; i < interpath.Length; i++)//循环每个dll文件
            {
    
    
                Assembly asm = Assembly.LoadFile(interpath[i]);
                //获取程序集里的所有公共类
                Type[] types=asm.GetExportedTypes();
                typeInterface = typeof(InterfaceByNote);//获取接口的type
                for (int r = 0; r < types.Length; r++)
                {
    
    
                    //循环遍历每个类,判断当前类是否实现了接口
                    if (typeInterface.IsAssignableFrom(types[r])&&!types[r].IsAbstract)
                    {
    
    
                        editor = (InterfaceByNote)Activator.CreateInstance(types[r]);//创建对象并保存到接口类型集合
                        ToolStripItem T=this.编辑EToolStripMenuItem.DropDownItems.Add(editor.Name.ToString());
                        plugins.Add(editor);
                        T.Tag = counts;
                        counts++;
                        T.Click += T_Click1;
                    }
                }
            }
            #endregion
        }
        private void T_Click1(object sender, EventArgs e)
        {
    
    
            ToolStripItem s=sender as ToolStripItem;
            InterfaceByNote st=(InterfaceByNote)plugins[Convert.ToInt32(s.Tag)];
            st.Run(this.textBox1);
        }

Extension interface file-
Insert picture description here
add extension assembly-
Insert picture description here

Guess you like

Origin blog.csdn.net/MrLsss/article/details/107029821