title: Get property type to pass to generic method
body: <p>I need to get the type of a property that will only be known at run time and pass this as a type parameter for a generic method. For example:</p>
  
  <pre><code>PropertyInfo prop = Bar.GetProperty("Property1");
  
  //"type 'prop' could not be found" error
  Foo&lt;prop.PropertyType&gt;();
  
  void Foo&lt;T&gt;()
  {
     //Stuff
  }
  class Bar
  {
     string Property1{get;set;}
  }
  </code></pre>
  
  <p>The type of Bar.Property1 will not be known at compile time, so I can't do <code>Foo&lt;string&gt;();</code>. It will compile and run correctly if I use <code>Foo&lt;dynamic&gt;();</code> but I'm not convinced that's the best way to go, and I'd like to know if there is a way to do it using an older framework.</p>
  
  <hr>
  
  <p>Hopefully, this more complete example will make my intentions clearer:</p>
  
  <pre><code>    public void Map(TInType inObject, TOutType outObject)
      {
          //propertyIn, propertyOut, and converter are all strings identifying the properties/methods to be used from the inObject/Type outObject/Type.
          SetPropertyValues&lt;dynamic, dynamic&gt;(inObject, outObject, propertyIn, propertyOut, converter);
      }
      private void SetPropertyValues&lt;TPropIn,TPropOut&gt;(TInType fromObject, TOutType toObject, string propertyIn, string propertyOut, string converter)
      {
          PropertyInfo prop1 = typeof(TInType).GetProperty(propertyIn);
          MethodInfo converterMethod = typeof(TInType).GetMethod(converter);
          PropertyInfo prop2 = typeof(TOutType).GetProperty(propertyOut);
  
          prop2.SetValue(
              toObject,
              CopyPropertyValue&lt;TPropIn, TPropOut&gt;((TPropIn)prop1.GetValue(fromObject, null), p =&gt; (TPropOut)converterMethod.Invoke(fromObject, new object[] { p })),
              null);
      }
      private TPropOut CopyPropertyValue&lt;TPropIn, TPropOut&gt;(TPropIn InPropValue, Func&lt;TPropIn, TPropOut&gt; converterFunction)
      {
          return converterFunction(InPropValue);
      }
  </code></pre>
  
  <p>I'm open to any other suggestions anyone may have, or that the code should be taken out back and shot, but my original question is still the one I'm most interested in.</p>