C#通用字典功能

时间:2014-10-22 08:02:33

标签: c# generics dictionary casting

您好我想创建一个动态通用输入框功能,允许用户从输入列表中选择一个输入然后返回。对于我想要使用字典的项目列表。

对于宣言我正在考虑以下几点:

public static DialogResult InputBox<KeyType, ValueType>(string aTitle, string aPromptText, Dictionary<KeyType, ValueType> aDictionary, ref  KeyValuePair<KeyType, ValueType> aReturnPair)

我已经有两个类似的功能,一个用于日期,一个用于字符串。

但我想知道字典是否可能? 我该如何处理铸造?我是否必须传递键和值的类型,或者我可以在运行时检索它吗?

也可以指定&#34;功能&#34;用于检索用于显示的名称例如,我有以下结构:

public struct Person
{
   public int Age;
   public string Name;
   public string Surname;
}

如何告诉函数使用Person.Surname进行显示?

使用示例:

Dictionary<int, Person> Persons = new Dictionary<int,Person>();
KeyValuePair<int, Person> lPair= new KeyValuePair<int,Person> 
DialogResult dialogResult = DialogResult.Cancel
While (dialogResult == DialogResult.Cancel)
{
    dialogResult = CustomForm.InputBox<int, Person>("title", "prompt", Persons, ref lPair, Person.Surname);
}

字符串输入功能

public static DialogResult InputBox(string aTitle, string aPromptText, ref string aValue, Boolean aPassword = false )
    {
        Form lForm = new Form();
        Label lLabel = new Label();
        TextBox lTextBox = new TextBox();
        Button lButtonOk = new Button();
        Button lButtonCancel = new Button();

        lForm.Text = aTitle;
        lLabel.Text = aPromptText;
        lTextBox.Text = aValue;

        if (aPassword)
        {
            lTextBox.PasswordChar = '*';
        }
        lButtonOk.Text = "OK";
        lButtonCancel.Text = "Cancel";
        lButtonOk.DialogResult = DialogResult.OK;
        lButtonCancel.DialogResult = DialogResult.Cancel;

        lLabel.SetBounds(9, 20, 372, 13);
        lTextBox.SetBounds(12, 36, 372, 20);
        lButtonOk.SetBounds(228, 72, 75, 23);
        lButtonCancel.SetBounds(309, 72, 75, 23);

        lLabel.AutoSize = true;
        lTextBox.Anchor = lTextBox.Anchor | AnchorStyles.Right;
        lButtonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        lButtonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        lForm.ClientSize = new Size(396, 107);
        lForm.Controls.AddRange(new Control[] { lLabel, lTextBox, lButtonOk, lButtonCancel });
        lForm.ClientSize = new Size(Math.Max(300, lLabel.Right + 10), lForm.ClientSize.Height);
        lForm.FormBorderStyle = FormBorderStyle.FixedDialog;
        lForm.StartPosition = FormStartPosition.CenterScreen;
        lForm.MinimizeBox = false;
        lForm.MaximizeBox = false;
        lForm.AcceptButton = lButtonOk;
        lForm.CancelButton = lButtonCancel;

        DialogResult dialogResult = lForm.ShowDialog();
        aValue = lTextBox.Text;
        return dialogResult;
    }

2 个答案:

答案 0 :(得分:2)

您可以使用委托函数将字典作为参数并将字符串返回给用户,如下所示:

Func<Dictionary<Object, Object>,string> userOutput

通过将此参数添加到函数中,您可以编写转换逻辑以及如何向用户提示输出,您可以这样调用它:

txtTextBox.Text = userOutput(dictionary);

答案 1 :(得分:0)

我设法创建了一个这是已完成的功能

public static DialogResult InputBox<KeyType, ValueType>(string aTitle, string aPromptText, Dictionary<KeyType, ValueType> aDictionary, ref  KeyValuePair<KeyType, ValueType> aReturnPair, Func<ValueType, string> aDelegate)
    {
        Form lForm = new Form();
        Label lLabel = new Label();
        ComboBox lComboBox = new ComboBox();
        Button lButtonOk = new Button();
        Button lButtonCancel = new Button();

        lForm.Text = aTitle;
        lLabel.Text = aPromptText;
        foreach (KeyValuePair<KeyType, ValueType> lPair in aDictionary)
        {
            lComboBox.Items.Add(aDelegate(lPair.Value));
        }


        lButtonOk.Text = "OK";
        lButtonCancel.Text = "Cancel";
        lButtonOk.DialogResult = DialogResult.OK;
        lButtonCancel.DialogResult = DialogResult.Cancel;

        lLabel.SetBounds(9, 20, 372, 13);
        lComboBox.SetBounds(12, 36, 372, 20);
        lButtonOk.SetBounds(228, 72, 75, 23);
        lButtonCancel.SetBounds(309, 72, 75, 23);

        lLabel.AutoSize = true;
        lComboBox.Anchor = lComboBox.Anchor | AnchorStyles.Right;
        lButtonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        lButtonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        lForm.ClientSize = new Size(396, 107);
        lForm.Controls.AddRange(new Control[] { lLabel, lComboBox, lButtonOk, lButtonCancel });
        lForm.ClientSize = new Size(Math.Max(300, lLabel.Right + 10), lForm.ClientSize.Height);
        lForm.FormBorderStyle = FormBorderStyle.FixedDialog;
        lForm.StartPosition = FormStartPosition.CenterScreen;
        lForm.MinimizeBox = false;
        lForm.MaximizeBox = false;
        lForm.AcceptButton = lButtonOk;
        lForm.CancelButton = lButtonCancel;

        DialogResult dialogResult = lForm.ShowDialog();
        if ((dialogResult == DialogResult.OK) && (lComboBox.SelectedIndex > -1) && (lComboBox.SelectedIndex < aDictionary.Count))
        {
            foreach (KeyValuePair<KeyType, ValueType> lPair in aDictionary)
            {
                if (string.Compare(aDelegate(lPair.Value),lComboBox.Items[lComboBox.SelectedIndex].ToString())== 0)
                {
                    aReturnPair = lPair;
                }
            }

        }            
        return dialogResult;
    }

您可以按如下方式调用它:

DialogResult dialogResult = CustomForm.InputBox<int, Person>("title", "prompt", Persons, ref lPair,  (Person) => Person.Name);