Declaring Func <in T, out Result> dynamisch

Bedenken Sie

var propertyinfo = typeof(Customer).GetProperty(sortExpressionStr);
Type orderType = propertyinfo.PropertyType;

Jetzt möchte ich @ deklarier

Func<int,orderType>

Ich weiß, dass es nicht direkt möglich ist, daordertype ist zur Laufzeit, aber gibt es eine Problemumgehung?

dies ist genau das, was ich tun möchte:

var propertyinfo = typeof(T).GetProperty(sortExpressionStr);
Type orderType = propertyinfo.PropertyType;

var param = Expression.Parameter(typeof(T), "x");
var sortExpression = (Expression.Lambda<Func<T, orderType>>
   (Expression.Convert(Expression.Property(param, sortExpressionStr), typeof(orderType)), param));

all das, weil ich konvertieren möchte:

Expression<Func<T,object>> to Expression<Func<T,orderType>>

oder wenn es nicht möglich ist, dann möchte ich es von Anfang an mit dem richtigen Typ erstellen, der Fall ist wie folgt:

Ich bin in einer Methode, die ein @ habtype(Customer) und einen Eigenschaftsnamen dieses Typs, nach dem ich sortieren möchte. Ich möchte einen Sortierausdrucksbaum erstellen, um ihn an @ zu übergebeOrderby (Hier)

Antworten auf die Frage(12)

Ihre Antwort auf die Frage