我尝试搜索此异常,但在我的案例中找不到任何解决方案
我使用下面的代码来调用 .NET 应用程序:
Assembly assem = Assembly.Load(Data);
MethodInfo method = assem.EntryPoint;
var o = Activator.CreateInstance(method.DeclaringType);
method.Invoke(o, null);
将被调用的应用程序有一个表单,并且在应用程序的入口点中:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); //Exception
Application.Run(new Form1());
}
SetCompatibleTextRenderingDefault
必须在第一个之前调用IWin32Window
对象在应用程序中创建。
EDIT :
Assembly a = Assembly.Load(Data);
MethodInfo method = a.GetType().GetMethod("Start");
var o = Activator.CreateInstance(method.DeclaringType);
method.Invoke(o, null);
您应该创建一个新方法来跳过初始化并通过反射查找Start
方法。但是Application.Start
会阻塞当前线程。如果您不想启动新的消息泵,则应该尝试使用反射查找 Form 类。
class Program
{
static void Main(string[] args)
{
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var filename = Path.Combine(path, "WindowsFormsApplication1.exe");
var assembly = Assembly.LoadFile(filename);
var programType = assembly.GetTypes().FirstOrDefault(c => c.Name == "Program"); // <-- if you don't know the full namespace and when it is unique.
var method = programType.GetMethod("Start", BindingFlags.Public | BindingFlags.Static);
method.Invoke(null, new object[] { });
}
}
和加载组件:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Start();
}
public static void Start() // <-- must be marked public!
{
MessageBox.Show("Start");
Application.Run(new Form1());
}
}
这在这里有效!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)