【问题标题】:C# Generic dictionary functionC# 通用字典函数
【发布时间】:2014-10-22 08:02:33
【问题描述】:

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

对于声明,我的思路如下:

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

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

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

也可以指定“函数”来检索用于显示的名称例如我有以下结构:

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;
    }

【问题讨论】:

  • 哪里需要用到字典?
  • 这个想法是覆盖字符串函数,仅用于字典。我希望能够给函数一个字典,然后它会提示用户输入一个部分。它适用于我正在创建的用于我们所有应用程序的库。这就是为什么我想要动态的键和值
  • 请检查我的编辑

标签: c# generics dictionary casting


【解决方案1】:

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

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

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

txtTextBox.Text = userOutput(dictionary);

【讨论】:

  • 我不明白你的意思。我想和我的字符串输入函数一样。我已将其附加到主帖。
  • 嗯,我现在明白你的意思了,谢谢。但是,如果我想这样称呼它,我将如何进行声明:Person.Surname 这是原始帖子中的示例。
  • 我添加了一个我想如何使用它的示例
【解决方案2】:

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

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);

【讨论】:

  • 是的,这样的事情你需要使用 Func 来解决它,如果你发现我的答案正确,请将其标记为答案:)
猜你喜欢
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
相关资源
最近更新 更多