求教,C# ,怎么通过button_Click事件跳转到另一个命名空间的form
怎么通过button_Click事件跳转到另一个命名空间的form,求教,怎么实现。程序代码:
//NamespaceJumpA.FormMain.cs using System; using System.Reflection; using System.Windows.Forms; namespace NamespaceJumpA { public partial class FormMain : Form { #region 常量 private const string Filter = "exe文件|*.exe"; #endregion #region 全局字段 private FormTarget _formTarget; #endregion #region 构造函数 public FormMain() { InitializeComponent(); } #endregion #region 控件事件 private void ButJumFar_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog { Filter = Filter }; if (ofd.ShowDialog() != DialogResult.OK) return; var assembly = Assembly.LoadFile(ofd.FileName); var types = assembly.GetTypes(); foreach (var type in types) { if(type == null || type.FullName == null) continue; if (!type.FullName.Contains("FormMain")) continue; var instance = Activator.CreateInstance(type); var method = type.GetMethod("DoSomething"); if (method == null) return; method.Invoke(instance, new object[0]); } } private void butJumpNear_Click(object sender, EventArgs e) { if (_formTarget == null) _formTarget = new FormTarget(); _formTarget.DoSomehing(); } #endregion } } //NamespaceJumpA.FormTarget.cs using System.Windows.Forms; namespace NamespaceJumpA { public partial class FormTarget : Form { #region 常量 private const string Msg = "I am in near target."; #endregion #region 构造函数 public FormTarget() { InitializeComponent(); } #endregion #region 公共方法 public void DoSomehing() { MessageBox.Show(Msg); } #endregion } } //NamespaceJumB.FormMain.cs using System.Windows.Forms; namespace NamespaceJumpB { public partial class FormMain : Form { #region 常量 private const string Msg = "I am in far target."; #endregion #region 构造函数 public FormMain() { InitializeComponent(); } #endregion #region 公共方法 public void DoSomething() { MessageBox.Show(Msg); } #endregion } }