c# - 调用使用Using语句打开的子表单的方法

标签 c# reflection using-statement

我试图弄清楚如何调用在 using block 中打开的子表单的方法。

在我的代码中,我有一个 MainForm,它是 MDI 父级。 MDI 父级保存父级窗体的引用。声明以下方法的父表单。

  1. ParentForm 实例以 MDIParent 形式提供,如以下代码所示。

    MainForm.cs

     private void OpenParentForm()
     {
         try
         {
    
             FormParent frmParent = null;
             frmParent = Application.OpenForms["frmParent"] as FormParent;
    
             if (frmParent != null)
             {
                 frmParent.Focus();
             }
             else
             {
                 frmParent = new FormParent();
                 frmParent.Name = "frmParent";
                 frmParent.MdiParent = this;
                 frmParent.Show();
             }
    
         }
         catch { }
     }
    
  2. 子表单打开于

    frmParent.cs

     private void ShowChildForm()
     {
         try
         {
             using (ChildForm frmChild = new ChildForm())
             {
    
                 if (frmChild.ShowDialog() == DialogResult.OK)
                     this.RefreshData();
    
                 frmChild.Dispose();
             }
         }
         catch { }
     }
    

ChildForm具有以下方法。

ChildForm.cs

private void ChildMethod()
{
    //........
    //........
    //........
}

以下是我的 InvokeMethod

ReflectionHelper.cs

public static async Task<bool> InvokeMethod(Form Window, string methodName, object param)
{
    try
    {
        Form mainWindow = Window;
        var MainWindowtype = mainWindow.GetType();
        MethodInfo MethodtoInvoke = MainWindowtype.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        if (MethodtoInvoke != null)
        {
            ParameterInfo[] pars = MethodtoInvoke.GetParameters();
            object[] Parameter = null;

            try
            {
                object returnValue = null;
                if (pars != null && pars.Length > 0)
                {
                    Parameter = new object[pars.Length];
                    foreach (var item in pars)
                    {
                        int index = item.Position;
                        if (param != null)
                        {
                            if (param.GetType() == item.ParameterType || item.ParameterType == typeof(object))
                            {
                                Parameter[index] = param;
                            }
                        }
                    }
                    returnValue = MethodtoInvoke.Invoke(mainWindow, Parameter);
                  
                }
                else
                    returnValue = MethodtoInvoke.Invoke(mainWindow, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception InvokeMethod  \n" + ex.Message);
            }
        }
    }
    catch (Exception ex)
    {
        return false;
    }
    return true;
}

目前,我可以调用 MainForm 中提供的 Parentform 实例方法。

Helper.cs

public class Helper
      {
        private async void OpenParentForm(string MethodName)
        {
        ReflectionHelper.InvokeMethod(static Instance of MainForm, MethodName, null);
        }
        private async void ShowChildForm(string MethodName)
        {
        ReflectionHelper.InvokeMethod(Instance of frmParent, MethodName, null);
        }
        private async void ChildMethod(string MethodName)
        {
        Invoke ChildMethod ???
        }
      }

CallingClass.cs

 class CallingClass
        {
            public async Task<bool> CallMethod(string MethodName)
            {
                switch (MethodName)
                {
                    case "OpenParentForm":
                        Helper.OpenParentForm(MethodName);
                        break;
                    case "ShowChildForm":
                        Helper.ShowChildForm(MethodName);
                        break;
                    case "ChildMethod":
                        Helper.ChildMethod(MethodName);
                        break;
                }
                return true;
            }
        }

我想调用MainForm>>frmParent>>[使用frmParent.cs block 中的ChildForm实例].ChildMethod()

最佳答案

您可以使用 Application.OpenForms["FormName"] 获取已打开表单的实例

示例:

ChildForm frmChild = Application.OpenForms["ChildForm"] as ChildForm;

然后您可以在 InvokeMethod 中将该实例作为参数传递,以调用您的 ChildForm 方法。

ReflectionHelper.InvokeMethod(frmChild , MethodName, null);

您的 Helper.cs 文件将如下所示:

public class Helper
{
        .....
        .....
        .....
        .....
        private async void ChildMethod(string MethodName)
        {
            ChildForm frmChild = Application.OpenForms["ChildForm"] as ChildForm;
            if(frmChild!=null)
              ReflectionHelper.InvokeMethod(frmChild , MethodName, null);
        }
 }

关于c# - 调用使用Using语句打开的子表单的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70492527/

相关文章:

c# - 如何在aes加密和解密算法中使用随机salt和随机iv?

c# - 如何获得实际的请求执行时间

c# - 静态类的 TypeAttribute

java - 如何执行已上传的类的方法?

c# - 处理命令并直接在命令上定义连接时,连接是否关闭?

c# - 使用 HelixToolkit 为 PointCloud 中的每个点应用颜色

java - 如何反射(reflect)实现map接口(interface)的字段?

c# - try/catch + using,正确的语法

c# - C# 的 using 语句中止安全吗?

c# - WPF 窗口应该是 native 所有者窗口的模式,但不是