我就是这样做的:
byte[] bytes = File.ReadAllBytes(@Application.StartupPath+"/UpdateMainProgaramApp.exe");
Assembly assembly = Assembly.Load(bytes);
// load the assemly
//Assembly assembly = Assembly.LoadFrom(AssemblyName);
// Walk through each type in the assembly looking for our class
MethodInfo method = assembly.EntryPoint;
if (method != null)
{
// create an istance of the Startup form Main method
object o = assembly.CreateInstance(method.Name);
// invoke the application starting point
try
{
method.Invoke(o, null);
}
catch (TargetInvocationException e)
{
Console.WriteLine(e.ToString());
}
}
问题是它抛出了TargetInvocationException
-它发现方法是main,但它抛出了这个异常,因为在这一行:
object o = assembly.CreateInstance(method.Name);
o
保持为空。我对这个堆栈跟踪做了一些研究实际的错误是:
InnerException = {"SetCompatibleTextRenderingDefault应该在程序中创建firstfirst IWin32Window对象之前被调用"}(这是我的翻译,因为它给了我一半希伯来语一半英语的堆栈跟踪,因为我的windows是希伯来语)
我做错了什么?
入口点方法是静态的,因此应该使用"instance"参数的空值来调用它。试着在组装后更换所有东西。载重线:
assembly.EntryPoint.Invoke(null, new object[0]);
如果入口点方法不是公共的,您应该使用允许您指定BindingFlags
的Invoke重载。
如果你检查任何WinForm application Program.cs文件,你会看到总是有这两行
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
也需要在程序集中调用它们。至少你的异常是这么说的
在它自己的进程中调用它如何?