C#之反射

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/sinat_27305053/article/details/80605104
            //动态加载程序集
            Assembly assembly = Assembly.LoadFile(dllPath);
            Type typeClass = assembly.GetType("TestNameSpace.Test"); //完整命名空间

            object obj= System.Activator.CreateInstance(typeClass);       // 创建实例

            if (typeClass!=null)

            {
                MethodInfo method = typeClass.GetMethod("TestMethodName", new Type[] { });      // 获取方法信息,没有传入参数
                object[] parameters = null;
                method.Invoke(obj, parameters);//调用方法

                method = typeClass.GetMethod("ShowEmrEdit", new Type[] { typeof(string) });      // 获取方法信息,传入参数为string类型
                parameters = new object[] { strContent }; ;
                method.Invoke(obj, parameters);//调用        

                PropertyInfo property = typeClass.GetProperty("xmlPath");
                property.SetValue(typeClass, xmlPath, null);//给变量赋值

            }


  //获取指定名称的类型
            Type typeForm = assembly.GetType("TestNameSpace.View.frmMain");

                if (typeof(Form).IsAssignableFrom(typeForm))//反射结果是否为Form

                {
                    Form frm = (Form)Activator.CreateInstance(typeForm);//创建反射窗体实例
                    frm.ShowDialog();
                }





            //获取指定名称的类型
            //Type typeForm = assembly.GetType("JHOODFuncIntegration.View.frmMain");

猜你喜欢

转载自blog.csdn.net/sinat_27305053/article/details/80605104
今日推荐