我在 C# 中有 2 个 windowsForm 项目(项目 A 和 B),但我想通过代码在项目 B 中添加对项目 A 的引用并从项目 B 中调用项目 A。我使用了 Assembly.Load,它只在工作如果我删除 Main void 参数。
项目 A 的表单应作为项目 B 的 MdiParent 打开。
我尝试使用 Assembly.load 和 activator.createinstance 但是当我尝试传递方法参数时它不起作用。
使用参数 args返回错误(System.MissingMethodException: 'Constructor in type' CompareXMLTools.Main 'not found.')
#using System.Reflection.Assembly
项目 A 程序.cs
namespace CompareXMLTools
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main(args));
}
}
}
窗体
namespace CompareXMLTools
{
public partial class Main : Form
{
public Main(string[] args)
{
InitializeComponent();
ArgsPassed = args;
}
}
}
项目 B
namespace XMLValidator
{
public partial class frmMain : Form
{
public frmMain(string[] args)
{
InitializeComponent();
ArgsPassed = args;
}
}
private void tsbrnCompareXML_Click(object sender, EventArgs e)
{
object dllUserControl = null;
System.Reflection.Assembly assembly2 = AppDomain.CurrentDomain.Load(File.ReadAllBytes(@"D:\Projetos C#\XMLValidator\XMLValidator\CompareXMLTools.exe"));
dllUserControl = assembly2.CreateInstance("CompareXMLTools.Main", true, System.Reflection.BindingFlags.Default, null, new string[] { }, System.Globalization.CultureInfo.CurrentCulture, null);
((frmMain)dllUserControl).MdiParent = this;
((frmMain)dllUserControl).Show();
}
}
注意:项目 B 命令仅在我string [] args
从主方法中删除 ** ** 字段时才有效。
我在调用项目 A 的新 WinForm 时需要传递参数,我该怎么做?