is there any way to set Func<>
type arguments dynamically, so i don't have to use endless if
statements?
Something like:
Type t = Type.GetType("System.Decimal");
Func<t> foo = new Func<t>(some_function);
Instead of:
Func<Decimal> foo = new Func<Decimal>(some_function);
UPDATE:
Here's a snippet from my 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<Func<StavkaDokumenta, decimal>>(final, pe);
o = lambda.Compile().Invoke(stavka);
}
if (t == typeof(decimal?))
{
var lambda = Expression.Lambda<Func<StavkaDokumenta, decimal?>>(final, pe);
o = lambda.Compile().Invoke(stavka);
}
else if (t == typeof(int))
{
var lambda = Expression.Lambda<Func<StavkaDokumenta, int>>(final, pe);
o = lambda.Compile().Invoke(stavka);
}
else if (t == typeof(int?))
{
var lambda = Expression.Lambda<Func<StavkaDokumenta, int?>>(final, pe);
o = lambda.Compile().Invoke(stavka);
}
else if (t == typeof(string))
{
var lambda = Expression.Lambda<Func<string>>(final, null);
o = lambda.Compile().Invoke();
}
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.