我正在从项目A获取所有“表单名称”到项目B中,如
我正在使用@jerry的答案。
当我的代码到达var f = (Form)emptyCtor.Invoke(new object[] { });
一行时,我得到的异常是调用错误的目标抛出的。
下面是完整的代码
try
{
Assembly projectA = Assembly.Load("ProjectA"); // replace with actual ProjectA name
// despite all Microsoft's dire warnings about loading from a simple name,
// you should be fine here as long as you don't have multiple versions of ProjectA
// floating around
foreach (Type t in projectA.GetTypes())
{
if (t.BaseType == typeof(Form))
{
var emptyCtor = t.GetConstructor(Type.EmptyTypes);
if (emptyCtor != null)
{
var f = (Form)emptyCtor.Invoke(new object[] { });
// t.FullName will help distinguish the unwanted entries and
// possibly later ignore them
string formItem = t.FullName + " // " + f.Text + " // " + f.Name;
checkedListBox1.Items.Add(formItem);
}
}
}
}
catch(Exception err)
{
// log exception
}
我被搜索为错误原因,但失败了。请建议我,我很累
编辑 :
以下是错误详细信息
图片1
图片2
试试这个:
var f = (Form)emptyCtor.Invoke(null);
顺便说一句,您要达到什么目标?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句