title: Reading method values at runtime using reflection (+hack)?
body: <p><em>Brief:</em> </p>
  
  <p>We have a C# method which accept parameters. </p>
  
  <p>inside of that method we need to add Sql parameters (according to the <em>method</em> parameters).</p>
  
  <p>It  actually looks like this : 
  <a href="https://i.stack.imgur.com/QP1y2.jpg" rel="nofollow noreferrer">http://i.stack.imgur.com/QP1y2.jpg</a> (to enlarge)
  <img src="https://i.stack.imgur.com/QP1y2.jpg" alt="enter image description here"></p>
  
  <p><strong>Please notice</strong></p>
  
  <p>I can write a class which will have all the method arguments. But then , I will have to create a class container for each SP ( and we have a lot).</p>
  
  <p>Ok lets continue.</p>
  
  <p>I was thinking to myself : "<strong>WHY</strong> should I <a href="http://msdn.microsoft.com/en-us/library/bb748677%28v=pandp.31%29.aspx" rel="nofollow noreferrer">insert each param manually</a> - If I already have its : <code>name</code> , <code>type</code> , <code>value</code> in the method param ? I don't want to do this , maybe reflection will do this for me"  ( <em>lets leave for now the conversion for <code>int &lt;-&gt;DbType.Int32</code></em>)  </p>
  
  <p>But I encountered a problem. it is <strong>not</strong> possible reading values using reflection.</p>
  
  <p>However  , I <strong>did</strong> managed to read values via reflection with a hack.</p>
  
  <p>1) I did this example:  ( simulation to the real scenario)</p>
  
  <pre><code>public static void MyMethod(int AddressId, string countryId, DateTime dt) //these are the arguments which need to be attached later
  {
  
   MethodBase mi = MethodInfo.GetCurrentMethod();
   var hack = new {AddressId, countryId, dt}; //this is the HACK
  
  
  var lstArguments = mi.GetParameters()  //get the parameter  name and types
                      .Select(p =&gt; new {
                                   name = p.Name  , type=p.GetType().Name
                                   }).ToList();
  
  
  List &lt; dynamic &gt; lstParamValues= new List &lt; dynamic &gt; ();
  
  foreach(PropertyInfo pi in hack.GetType().GetProperties())  //get the value(!!!) of each param 
  {
  
         lstParamValues.Add(pi.GetValue(hack, null));
  }
  
  
    dynamic dyn= myDBInstance; // get dynamic reference to the DB class instance ( 
  
  for(int i = 0; i &lt; lstArguments .Count; i++) //dynamically Invoke the method with param name+value.
  {
      dyn.AddInParameter(lstArguments [i].name, ((lstParamValues[i] is int) ? DbType.Int32 : DbType.String), lstParamValues[i]);
  }
  
  }
  </code></pre>
  
  <p>And  there is the <strong>DB class</strong> ( to <strong>simulate</strong> the <code>AddInParameter</code> to see if im getting values)</p>
  
  <pre><code>class myDB
  {
      public void AddInParameter(string name, DbType dbt, object val)
      {
          Console.WriteLine("name=" + name + "    " + "DbType=" + dbt + "    " + "object val=" + val);
      }
  }
  </code></pre>
  
  <p><strong>And it is working :</strong> </p>
  
  <p>I executed the method with :</p>
  
  <pre><code>MyMethod(1, "2", DateTime.Now);
  </code></pre>
  
  <p>And the output was : </p>
  
  <pre><code>name=AddressId    DbType=Int32    object val=1
  name=countryId    DbType=String    object val=2
  name=dt    DbType=String    object val=05/02/2013 19:09:32
  </code></pre>
  
  <p>All fine.</p>
  
  <p><strong>the C# Question</strong></p>
  
  <p>How can I get over this hack : </p>
  
  <pre><code> var hack = new {AddressId, countryId, dt};  //  the method arguments names
  </code></pre>
  
  <p>In my sample I wrote it hard coded.</p>
  
  <p>The reflection method <code>GetValue</code> is working only because of this line.</p>
  
  <p>Is there anything I can do to add on run-time the method arguments name as  a property  , so I will be able to do : </p>
  
  <pre><code> foreach(PropertyInfo pi in SOMETHING.GetType().GetProperties()) 
     {
     }
  </code></pre>
  
  <p>Please consider this question as a c# question and not ORM question.</p>
  
  <p>Also , If you want to play with running sample code ( just paste in console proj) : <a href="http://jsbin.com/azicut/1/edit" rel="nofollow noreferrer">http://jsbin.com/azicut/1/edit</a></p>