C#学习教程:如何将程序集加载到内存中并执行UpdateMainProgaramApp.exe");程序集assembly=Assembly.Load(bytes);//加载程序集//Assemblyassembly=Assembly.LoadFrom(AssemblyName);//遍历程序集中的每个类型以查找我们的类MethodInfomethod=assembly.EntryPoint;if(method!=null){//创建Startup窗体的实例Mainmethodobjecto=assembly.CreateInstance(method.Name);//调用应用程序起点try{method.Invoke(o,null);}catch(TargetInvocationExceptione){Console.WriteLine(e.ToString());问题是它抛出TargetInvocationException-它发现该方法是主要的,但它抛出这个异常,因为在这一行中:objecto=assembly.CreateInstance(method.Name);o保持为空。所以我在堆栈跟踪中挖掘了一下,实际错误是这样的:InnerException={"SetCompatibleTextRenderingDefaultshouldbecalledbeforethefirstIWin32Windowobjectiscreatedintheprogram"}(这是我的翻译,因为它给了我一半希伯来语一半英语堆栈跟踪因为我的窗口是希伯来语。)我究竟做错了什么?入口点方法是静态的,因此应该使用“instance”参数的空值来调用它。尝试将Assembly.Load行之后的所有内容替换为:assembly.EntryPoint.Invoke(null,newobject[0]);如果入口点方法不是公共的,您应该使用允许您指定BindingFlags的Invoke重载。如果您检查任何WinForm应用程序Program.cs文件,您将始终看到这两行Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);您需要在程序集中调用它们。至少你的例外是这么说的。如何在自己的程序中调用它?以上就是C#学习教程:如何将程序集加载到内存中,并执行它共享的所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
