title: Pass property itself to function as parameter in C#
body: <p>I am looking for a method to pass property itself to a function. Not value of property. Function doesn't know in advance which property will be used for sorting. Simplest way in this example is: creating 4 overwrites with different parameter types. Other way is using of <code>typeof()</code> inside function. Both these ways are unacceptable when Class1 has hundreds properties. So far I found following method:</p>
  
  <pre><code>class Class1
  {
      string vehName;
      int maxSpeed;
      int fuelCapacity;
      bool isFlying;
  }
  
  class Processor
  {
      List&lt;Class1&gt; vehicles = null;
      Processor(List&lt;Class1&gt; input)
      {
          vehicles = input;
      }
  
      List&lt;Class1&gt; sortBy(List&lt;Class1&gt; toSort, string propName)
      {
          if (toSort != null &amp;&amp; toSort.Count &gt; 0)
          {
              return toSort.OrderBy(x =&gt; typeof(Class1).GetProperty(propName).GetValue(x, null)).ToList();
          }
          else return null;
      }
  }
  
  class OuterUser
  {
      List&lt;Class1&gt; vehicles = new List&lt;Class1&gt;();
      // ... fill the list
      Processor pr = new Processor(vehicles);
      List&lt;Class1&gt; sorted = pr.sortBy("maxSpeed");
  }
  </code></pre>
  
  <p>I don't like this method because of risk of "human error" when passing string to processing function. When the string is generated by other part of code this is going be even more ugly.
  Please, suggest more elegant way to implement passing of Class1 property to function for further processing. The best option for usage IMHO will be (or something like this):</p>
  
  <pre><code>vehicles = sortBy(vehicles, Class1.maxSpeed);
  </code></pre>