Eli*_*kiy 6 c# reflection winforms
这就是我在做什么:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
问题是它抛出TargetInvocationException- 它发现该方法是main,但它抛出此异常,因为在这一行:
object o = assembly.CreateInstance(method.Name);
Run Code Online (Sandbox Code Playgroud)
o保持为空.所以我在堆栈跟踪中挖了一下,实际的错误是这样的:
InnerException = {"SetCompatibleTextRenderingDefault应该在程序中创建第一个IWin32Window对象之前被调用"}(这是我的翻译,因为它给了我一半希伯来半英语的堆栈跟踪,因为我的窗口是希伯来语.)
我究竟做错了什么?
如果你检查任何 WinForm 应用程序 Program.cs 文件,你会看到总是有这两行
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Run Code Online (Sandbox Code Playgroud)
您还需要在程序集中调用它们。至少这是你的例外所说的。