使用嵌套的 class 将多个参数传递给后台工作者

Using a nested class to pass multiple arguments into a backgroundworker

我正在尝试使用嵌套的 class 将两个 classes 传递到一个参数中,这样我就可以将它发送给后台工作人员。到目前为止,我已经设法将单个参数传递给后台工作者,但我还没有使用嵌套的 class 来完成它,我最终将两个参数或我想要的 class 传递给同一个参数。到目前为止,这是我正在使用的一些代码:

这是我正在尝试使用的嵌套 Class:

public class MyBackGroundWorkerObject
{
    public class TimeZone
    {
        public string Zone;

        public int difference;

        public override string ToString()
        {
            return Zone;
        }
    }

    public class AccountName
    {
        public string AccountSid;
        public string AuthToken;
        public string Name;
        public override string ToString()
        {
            return Name;
        }    
    }

}

下面是一个 classes 的例子:

MyBackGroundWorkerObject.AccountName acct = new MyBackGroundWorkerObject.AccountName();
        //AccountName acct = new AccountName();
        acct.AccountSid = "abcd";
        acct.AuthToken = "xyz";
        acct.Name = "Potato";
        ddlAccounts.Items.Add(acct);

MyBackGroundWorkerObject.TimeZone region = new MyBackGroundWorkerObject.TimeZone();
        //TimeZone region = new TimeZone();
        region.Zone = "UTC";
        region.difference = 0;
        comboBox1.Items.Add(region);

这是我完全困惑的部分,我希望在从 Window 的表单调用时能够同时使用这两个表单来检索一些输入的数据。我不确定如何让这两个 class 结合起来工作,我可以同时将它们发送给后台工作人员:

MyBackGroundWorkerObject myBackGroundWorker1 = new MyBackGroundWorkerObject();
object obj = ddlAccounts.SelectedItem;

            MyBackGroundWorkerObject.AccountName acct = obj as MyBackGroundWorkerObject.AccountName; 
backgroundWorker1.RunWorkerAsync(acct);

您在 MyBackGroundWorkerObject 中定义了嵌套的 classes,但是在 MyBackGroundWorkerObject class.

中没有声明 TimeZone 类型或 AccountName 类型的变量
public class MyBackGroundWorkerObject
{
    public class TimeZone
    {
        public string Zone;

        public int difference;

        public override string ToString()
        {
            return Zone;
        }
    }

    public class AccountName
    {
        public string AccountSid;
        public string AuthToken;
        public string Name;
        public override string ToString()
        {
            return Name;
        }    
    }

    public TimeZone TheTimeZone {get; set;}
    public AccountName TheAccountName {get; set;}

}

现在您可以分别通过 TheTimeZone 和 TheAccountName 成员设置实例,并在传递 MyBackGroundWorkerObject 时访问它们。

MyBackGroundWorkerObject myBackGroundWorker1 = new MyBackGroundWorkerObject();
MyBackGroundWorkerObject.AccountName acct = new MyBackGroundWorkerObject.AccountName();
    //AccountName acct = new AccountName();
    acct.AccountSid = "abcd";
    acct.AuthToken = "xyz";
    acct.Name = "Potato";
    ddlAccounts.Items.Add(acct);

MyBackGroundWorkerObject.TimeZone region = new MyBackGroundWorkerObject.TimeZone();
    //TimeZone region = new TimeZone();
    region.Zone = "UTC";
    region.difference = 0;
    comboBox1.Items.Add(region);

myBackGroundWorker1.TheTimeZone = region;
myBackGroundWorker1.TheAccountName = acct;

backgroundWorker1.RunWorkerAsync(myBackGroundWorker1);

在后台工作人员内部,doWork 将其转换为 MyBackGroundWorkerObject 并再次通过 .TheTimeZone 和 .TheAccountName 访问它