title: How to avoid rewriting the same code in different methods
body: <blockquote>
    <p>OLD Question<br>
    So this is what I am trying to achieve...</p>
    
    <p>I have an existing abstract class ..lets name it Class1.cs . It
    contains definitions for a number of methods. So now I have included
    some new functionality which needs to be implemented in each and every
    method of the Class1 class. So for ex-</p>
  
  <pre><code>        public void Method_1(string arg1, string arg2)
          {
             /*
             //some code implementation specific to Method_1
             */
             Dictionary&lt;string, object&gt; dict= new Dictionary&lt;string, object&gt;();
             //there can be more or less arguments in other methods
             dict.Add("Arg1", arg1);
             dict.Add("Arg2", arg2);
             Method_2(dict);
          }
  </code></pre>
    
    <p>I have to do the exact same thing in all the methods but the arguments
    can vary. So the dictionary object can have "n" parameters . Is there
    a way that I can avoid the manual labor of adding the same code
    repeatedly (maybe use a design pattern if possible)</p>
    
    <p>i think i am not clear... bundling up the dictionary generation
    mechanism is not my concern , I would still need to add the same code
    in all the methods(around 50) ..I am trying to avoid manually calling
    the same code again and again 50 times...</p>
  </blockquote>
  
  <p><strong>Edited and reframed the question</strong><br>
  I have finally decided that I would build the dictionary in a private method and call that in all the other methods. Kindly ignore everything else before this paragraph.
  My methods will look like this</p>
  
  <pre><code>public void Method1(string a, string b , string c)
  { 
     Dictionary&lt;string,object&gt; dict = BuildDictionary(new {a, b ,c}); 
     /*the dict object should have the following structure
                             key=a,   value=  value of a 
                             key =b , value = value of b
                             key =b , value = value of b*/
  }
  public void Method2(string x, string y)
  { 
     Dictionary&lt;string,object&gt; dict = BuildDictionary(new {x,y}); 
     /*the dict object should have the following structure
                             key= x,  value=  value of x 
                             key =y , value = value of y */
  }
  private Dictionary&lt;string,object&gt; BuildDictionary(params object[] values)
  {
     //values.ToString() gives  = { a= "Value of a", b== "Vaue of b", c= "Value of c"}
     //Copy pasting Simon's Code here(use of anonymous objects)
     var dictionary = values.GetType()
                         .GetProperties()
                         .ToDictionary(pi =&gt; pi.Name, pi =&gt; pi.GetValue(values));
     //this dictionary object gives me a count of 7 with keys as the properties of the object datatype(which is not relevant to my case). 
  }
  </code></pre>
  
  <p>So what changes do i need to make to the BuildDictionary method in order to get the desired dictionary structure?</p>