I clarify the question (apparently either I did not understand you, or you did me). Here are the parts of the program

private void FormLoad(object sender, EventArgs e) { m_MethodCombo.Items.AddRange(new[]{"MD5", "SHA1", "MD160"}); } private void CalcClick(object sender, EventArgs e) { var method = (string)m_MethodCombo.SelectedItem; HashAlgorithm alg; switch (method) { case "MD5" : alg = MD5.Create(); break; case "SHA1" : alg = SHA1.Create(); break; case "MD160": alg = RIPEMD160.Create(); break; default : throw new ApplicationException("Unknown hash method"); } m_OutputBox.Lines = m_InputBox .Lines .Select(line => ComputeHash(alg, line)) .ToArray(); } 

It can be seen that MD160 corresponds to the RIPEMD160 hash algorithm. It is required that by the element from the drop-down list (for example, MD160) determine the name of the hash of the algorithm (in this case RIPEMD160). That is, in my Algorithm class there will be a constructor to which the item of the drop-down list will be passed, and in the constructor, somehow the field HashAlgorithm of my Algorithm class will be initialized using this passed argument. The question is how to do it (as I see the solution: to search through all HashAlgorithms and get some name of the hash functions by them with some of their methods, but I don’t know how to save them and how to get their names for the hash functions functions)?

and so that you can go through all existing algorithms (I don’t know if the iterator is applicable here) and get the names of the corresponding functions and compare the names of these hash functions with the selected item from the drop-down list, that is, you cannot do without creating your own dictionary?

    1 answer 1

    I didn’t understand you, but the essence of the dictionary remains:

    let's say you create an Algorithm class

    Add the private Dictionary<string, Func<HashAlgorithm>> d to this class and in the static constructor of the class or where else add another name to this word to the name of the algorithms and the methods of their creation themselves:

     d.Add("MD160", RIPEMD160.Create); d.Add("SHA1", SHA1.Create); d.Add("MD5", MD5.Create); 

    now the constructor:

     public HashAlgorithm AlgWhatYouNeed {get; private set;}//где-то в классе ... public Algorithm(string algName) { ... AlgWhatYouNeed = d[algName]();//вернёт именно тот Create, который вам нужен ... } 

    in your case:

     ... var method = (string)m_MethodCombo.SelectedItem; Algorithm alg = new Algorithm(method); ... m_OutputBox.Lines = m_InputBox .Lines .Select(line => ComputeHash(alg.AlgWhatYouNeed, line)) .ToArray(); 
    • and so that you can go through all existing algorithms (I don’t know if the iterator is applicable here) and get the names of the corresponding functions and compare the names of these hash functions with the selected item from the drop-down list, that is, you cannot do without creating your own dictionary? - seeker
    • Dictionaries are fast enough and lightweight objects, why not? Of the other options - in some sophisticated way to use reflection, but the hassle will be more ... - Specter
    • What is reflection? - seeker