A more object oriented approach:
Create an algorithm interface that will be displayed in your combo box:
public interface IAlgorithmItem { SymmetricAlgorithm CreateAlgorithm(); string DisplayName { get; } }
Then create a new class for each desired algorithm:
public class AesAlgorithm : IAlgorithmItem { public AesAlgorithm() { } public SymmetricAlgorithm CreateAlgorithm() { return Aes.Create(); } public string DisplayName { get { return "AES"; } } } public class RijndaelAlgorithm : IAlgorithmItem { public SymmetricAlgorithm CreateAlgorithm() { return Rijndael.Create(); } public string DisplayName { get { return "Rijndael"; } } }
Then you can create a new list of items:
var listItems = new List<IAlgorithmItem>() { new AesAlgorithm(), new RijndaelAlgorithm() };
Then you can bind the combo box to this list:
comboBox1.DataSource = listItems; comboBox1.DisplayMember = "DisplayName";
You can later refer to the selected item:
var algorithmItem = (IAlgorithmItem)comboBox1.SelectedItem; var algorithm = algorithmItem.CreateAlgorithm();
EDIT: Updated with Will's suggestion to use an interface rather than an abstract base class. EDIT 2: Updated to use the create method, not the property, as the result of the operation will create a new algorithm every time it is available.
source share