title: Dynamically set Func<> types
body: <p>is there any way to set <code>Func&lt;&gt;</code> type arguments dynamically, so i don't have to use endless <code>if</code> statements?</p>
  
  <p>Something like:</p>
  
  <pre><code>Type t = Type.GetType("System.Decimal");
  Func&lt;t&gt; foo = new Func&lt;t&gt;(some_function);
  </code></pre>
  
  <p>Instead of:</p>
  
  <pre><code>Func&lt;Decimal&gt; foo = new Func&lt;Decimal&gt;(some_function);
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Here's a snippet from my code:</p>
  
  <pre><code>Type t = typeof(StavkaDokumenta).GetProperty(pd.Polje).PropertyType;
  ParameterExpression pe = Expression.Parameter(typeof(StavkaDokumenta), "stavka");
  Expression expr = Expressions.ResolveCompleteExpression(pe, pd.Expression);
  Expression final = Expression.Convert(expr, t);
  if (t == typeof(decimal))
  {
      var lambda = Expression.Lambda&lt;Func&lt;StavkaDokumenta, decimal&gt;&gt;(final, pe);
      o = lambda.Compile().Invoke(stavka);
  }
  if (t == typeof(decimal?))
  {
      var lambda = Expression.Lambda&lt;Func&lt;StavkaDokumenta, decimal?&gt;&gt;(final, pe);
      o = lambda.Compile().Invoke(stavka);
  }
  else if (t == typeof(int))
  {
      var lambda = Expression.Lambda&lt;Func&lt;StavkaDokumenta, int&gt;&gt;(final, pe);
      o = lambda.Compile().Invoke(stavka);
  }
  else if (t == typeof(int?))
  {
      var lambda = Expression.Lambda&lt;Func&lt;StavkaDokumenta, int?&gt;&gt;(final, pe);
      o = lambda.Compile().Invoke(stavka);
  }
  else if (t == typeof(string))
  {
      var lambda = Expression.Lambda&lt;Func&lt;string&gt;&gt;(final, null);
      o = lambda.Compile().Invoke();
  }
  </code></pre>
  
  <p>pd.Polje is string - name of a property inside "StavkaDokumenta" class.
  pd.Expression is string expression that must evaluate to type of Polje.
  stavka is an instance of StavkaDokumenta.</p>