我有以下类(暴露给COM),我将其用作包装器来显示从VB6调用的表单。
public class NetForm : INetForm
{
public NetForm(Assembly assembly, string name, object[] parameters)
{
Assembly = assembly;
Name = name;
CreateInstance(parameters);
}
private Assembly Assembly { get; set; }
private string Name { get; set; }
private Form Instance { get; set; }
private void CreateInstance(object[] parameters)
{
Type type = Assembly.GetType(Name);
Type[] types = new Type[parameters.Length];
for (int idx = 0; idx < parameters.Length; idx++)
types[idx] = parameters[idx].GetType();
ConstructorInfo ci = type.GetConstructor(types);
Instance = (Form)ci.Invoke(parameters);
}
public void Show()
{
Instance.Visible = true;
Instance.Show(); // Crashing
}
public void ShowDialog()
{
Instance.ShowDialog(); // Works perfectly
}
}
public class NetAssembly
{
public NetAssembly(string fullname)
{
Init(fullname);
}
private Assembly Assembly { get; set; }
private void Init(string name)
{
Assembly = Assembly.LoadFrom(name);
}
public NetForm GetForm(string nameSpace, string name, object[] parameters)
{
name = string.Concat(nameSpace, ".", name);
return new NetForm(Assembly, name,parameters);
}
}
我正在使用UnitTestProject进行检查:
[TestClass]
public class NetFormUnitTest
{
[TestMethod]
public void ShowTestMethod()
{
var assembly = new NetAssembly(assemblyFullName);
var form = assembly.GetForm(nameSpace, formName, new object[] { "Item1" });
form.Show();
}
}
调用Show()方法的正确方法是什么?
答案 0 :(得分:1)
您的表单需要一个消息泵(例如 Application.Run 创建)。 ShowDialog的原因是它创建了自己的消息循环。
因此,添加Application.Run(formInstance)
可以解决问题。