C# 确定表格';s在运行时的标识(类型信息)

C# 确定表格';s在运行时的标识(类型信息),c#,casting,runtime,C#,Casting,Runtime,假设我有一个公共类,它执行一些耗时的步骤(例如,将内容保存到USB)。我希望能够从多个表单调用该代码,并在完成一个步骤时接收反馈。普通类如何知道向谁发送反馈?下面的代码描述了这种情况: // ### Common class frmCommon ### // Parent form (when feedback on some slow operation is required) private static Form m_frmParent = null; // ... public st

假设我有一个公共类,它执行一些耗时的步骤(例如,将内容保存到USB)。我希望能够从多个表单调用该代码,并在完成一个步骤时接收反馈。普通类如何知道向谁发送反馈?下面的代码描述了这种情况:

// ### Common class frmCommon ###
// Parent form (when feedback on some slow operation is required)
private static Form m_frmParent = null;

// ...
public static void SetParentForm(Form frmParent)
{
    // When some time consuming process takes place (such as saving to USB), setting the
    // parent form allows feedback to be given to the user (eg. as a progress bar)
    m_frmParent = frmParent;
}

public static void DoSomething()
{
    for (int nStep = 0; nStep < 100; nStep++)
    {
        // Tell the parent form how many product sets (groups of 20) there are to read
        if (m_frmParent != null)
        {
            // How to decide whether to call form 1 or form 2?
            ((frmForm1)m_frmParent).SendFeedback(nStep);
            ((frmForm2)m_frmParent).SendFeedback(nStep);
        }

        // Perform the time-consuming step...
        SlowStep(nStep);
    }
}

// ### FORM 1 frmForm1 ###
private void SomeEventForm1(int nStep)
{
    frmCommon.SetParentForm(this);
    frmCommon.DoSomething();
    frmCommon.SetParentForm(null);
}

public void SendFeedback(int nStep)
{
    // Do something like update a progress bar on form 1
    Application.DoEvents();
}

// ### FORM 2 frmForm2 ###
private void SomeEventForm2(int nStep)
{
    frmCommon.SetParentForm(this);
    frmCommon.DoSomething();
    frmCommon.SetParentForm(null);
}

public void SendFeedback(int nStep)
{
    // Do something like update a progress bar on form 2
    Application.DoEvents();
}
/###公共类frmCommon###
//父窗体(需要对某些缓慢操作进行反馈时)
私有静态形式m_frmParent=null;
// ...
公共静态无效SetParentForm(表格frmParent)
{
//当发生一些耗时的过程(如保存到USB)时,设置
//父窗体允许向用户提供反馈(例如作为进度条)
m_frmParent=frmParent;
}
公共静态无效剂量测定法()
{
对于(int nStep=0;nStep<100;nStep++)
{
//告诉父窗体有多少个产品集(20组)要读取
如果(m\u frmParent!=null)
{
//如何决定是否调用表格1或表格2?
发送反馈(nStep);
发送反馈(nStep);
}
//执行耗时的步骤。。。
SlowStep(nStep);
}
}
//####表格1 FRM表格1###
私有void SomeEventForm1(int nStep)
{
frmCommon.SetParentForm(本);
frmCommon.DoSomething();
frmCommon.SetParentForm(null);
}
公共无效发送反馈(int nStep)
{
//执行类似于更新表单1上的进度条的操作
Application.DoEvents();
}
//####表格2 FRM表格2###
私有void SomeEventForm2(int nStep)
{
frmCommon.SetParentForm(本);
frmCommon.DoSomething();
frmCommon.SetParentForm(null);
}
公共无效发送反馈(int nStep)
{
//执行类似于更新表单2上的进度条的操作
Application.DoEvents();
}

如果对.NET 2.0有影响的话,就瞄准它。

调用代码必须提供对该类的委托。当类完成耗时的过程时,它将调用该委托,以通知调用代码它已完成。请查找有关如何执行此操作的好教程。

调用代码必须提供该类的委托。当类完成耗时的过程时,它将调用该委托,以通知调用代码它已完成。请查找有关如何执行此操作的好教程。

调用代码必须提供该类的委托。当类完成耗时的过程时,它将调用该委托,以通知调用代码它已完成。请查找有关如何执行此操作的好教程。

调用代码必须提供该类的委托。当类完成耗时的过程时,它将调用该委托,以通知调用代码它已完成。寻找一个关于如何做到这一点的很好的教程。

1:如果代码> sEndiffs是在两种窗体中实现的函数,它们都是相同的,请考虑在<代码>静态类< /COD> > <强>扩展> < /强> <代码>窗体< /代码>:

中创建一个单<代码>静态<代码>方法。
public static class FormExtender
{
    public static void SendFeedback(this Form frm, int nStep)
   {
       //do what must be done
       //you can call this anyhere using, for instance: m_frmParent.SendFeedback(nStep)
       //when you call it like that, m_frmParent will be given to this function as the argument frm
   }

}
2-但如果两种形式的方法不同,我建议您创建一个接口:

interface IFormWithFeedback
{
    void SendFeedback(int nStep);
}
然后form1和form2应该实现这一点(只需在声明表单的地方添加
,IFormWithFeedBack
):

该类中的父表单应该是
IFormWithFeedback
,而不是表单:

 private static IFormWithFeedback m_frmParent = null;

这两个选项(扩展方法或接口)都允许您从m\u frmParent调用
SendFeedback
direclty,而无需强制转换它

>P>1——如果代码> sEddiffs是在两种窗体中实现的函数,它们都是相同的,请考虑在<代码>静态类< /代码>中创建单个<代码>静态<代码> >方法> <强>扩展< /强> <代码>窗体< /代码>:

public static class FormExtender
{
    public static void SendFeedback(this Form frm, int nStep)
   {
       //do what must be done
       //you can call this anyhere using, for instance: m_frmParent.SendFeedback(nStep)
       //when you call it like that, m_frmParent will be given to this function as the argument frm
   }

}
2-但如果两种形式的方法不同,我建议您创建一个接口:

interface IFormWithFeedback
{
    void SendFeedback(int nStep);
}
然后form1和form2应该实现这一点(只需在声明表单的地方添加
,IFormWithFeedBack
):

该类中的父表单应该是
IFormWithFeedback
,而不是表单:

 private static IFormWithFeedback m_frmParent = null;

这两个选项(扩展方法或接口)都允许您从m\u frmParent调用
SendFeedback
direclty,而无需强制转换它

>P>1——如果代码> sEddiffs是在两种窗体中实现的函数,它们都是相同的,请考虑在<代码>静态类< /代码>中创建单个<代码>静态<代码> >方法> <强>扩展< /强> <代码>窗体< /代码>:

public static class FormExtender
{
    public static void SendFeedback(this Form frm, int nStep)
   {
       //do what must be done
       //you can call this anyhere using, for instance: m_frmParent.SendFeedback(nStep)
       //when you call it like that, m_frmParent will be given to this function as the argument frm
   }

}
2-但如果两种形式的方法不同,我建议您创建一个接口:

interface IFormWithFeedback
{
    void SendFeedback(int nStep);
}
然后form1和form2应该实现这一点(只需在声明表单的地方添加
,IFormWithFeedBack
):

该类中的父表单应该是
IFormWithFeedback
,而不是表单:

 private static IFormWithFeedback m_frmParent = null;

这两个选项(扩展方法或接口)都允许您从m\u frmParent调用
SendFeedback
direclty,而无需强制转换它

>P>1——如果代码> sEddiffs是在两种窗体中实现的函数,它们都是相同的,请考虑在<代码>静态类< /代码>中创建单个<代码>静态<代码> >方法> <强>扩展< /强> <代码>窗体< /代码>:

public static class FormExtender
{
    public static void SendFeedback(this Form frm, int nStep)
   {
       //do what must be done
       //you can call this anyhere using, for instance: m_frmParent.SendFeedback(nStep)
       //when you call it like that, m_frmParent will be given to this function as the argument frm
   }

}
2-但如果两种形式的方法不同,我建议您创建一个接口:

interface IFormWithFeedback
{
    void SendFeedback(int nStep);
}
然后form1和form2应该实现这一点(只需在声明表单的地方添加
,IFormWithFeedBack
):

该类中的父表单应该是
IFormWithFeedback
,而不是表单:

 private static IFormWithFeedback m_frmParent = null;
这两个选项(扩展方法或接口)都允许您从m\u frmParent调用
SendFeedback
direclty,而无需强制转换它

我宁愿使用事件:

公共类慢进程{
...
//最简单,不是线程安全的
公共静态事件事件处理程序已更改;
公共静态无效剂量测定法(){
对于(int nStep=0;nStep<100;nStep++){