<p>I'm writing some code for a class constructor which loops through all the properties of the class and calls a generic static method which populates my class with data from an external API. So I've got this as an example class:</p>

<pre><code>public class MyClass{
  public string Property1 { get; set; }
  public int Property2 { get; set; }
  public bool Property3 { get; set; }

  public static T DoStuff&lt;T&gt;(string name){
    // get the data for the property from the external API
    // or if there's a problem return 'default(T)'
  }
}
</code></pre>

<p>Now in my constructor I want something like this:</p>

<pre><code>public MyClass(){
  var properties = this.GetType().GetProperties();
  foreach(PropertyInfo p in properties){
    p.SetValue(this, DoStuff(p.Name), new object[0]);
  }
}
</code></pre>

<p>So the above constructor will thrown an error because I'm not supplying the generic type.</p>

<p>So how do I pass in the type of the property in?</p>

Do you want to call DoStuff<T> with T = the type of each property? In which case, "as is" you would need to use reflection and MakeGenericMethod - i.e.
```
```
var properties = this.GetType().GetProperties();
foreach (PropertyInfo p in properties)
{
    object value = typeof(MyClass)
    .GetMethod("DoStuff")
    .MakeGenericMethod(p.PropertyType)
    .Invoke(null, new object[] { p.Name });
    p.SetValue(this, value, null);
}
```
```
However, this isn't very pretty. In reality I wonder if it wouldn't be better just to have:
```
```
static object DoStuff(string name, Type propertyType);
... and then
object value = DoStuff(p.Name, p.PropertyType);
```
```
What does the generics give you in this example? Note that value-types will still get boxed etc during the reflection call - and even then boxing
isn't as bad as you might think
.
Finally, in many scenarios, TypeDescriptor.GetProperties() is more appropriate than Type.GetProperties() - allows for flexible object models etc.