I want to set a value to a property using the PropertyInfo i currently have.
I can simply call PropertyInfo.SetValue
but this is slow and there would be a lot of setting to happen.
What i want is a compiled expression but i cant make it work without knowing the Type of the value to set.
Here is my current code.
private static Action<TObject, List<AnimalColor>> GetPropSetter<TObject>(PropertyInfo propertyInfo)
{
ParameterExpression paramExpression = Expression.Parameter(typeof(TObject));
ParameterExpression paramExpression2 = Expression.Parameter(propertyInfo.PropertyType, propertyInfo.Name);
MemberExpression propertyGetterExpression = Expression.Property(paramExpression, propertyInfo.Name);
Action<TObject, List<AnimalColor>> result = Expression.Lambda<Action<TObject, List<AnimalColor>>>
(
Expression.Assign(propertyGetterExpression, paramExpression2), paramExpression, paramExpression2
).Compile();
return result;
}
Consumed like
var setter = GetPropSetter<TResponse>(property);
setter(response, deserializedResponse as List<AnimalColor>);
But what i want is for it to know the type of the parameter to set, and just pass object.
public static Action<TObject, object> GetPropSetter<TObject, object>(string propertyName)
{
ParameterExpression paramExpression = Expression.Parameter(typeof(TObject));
ParameterExpression paramExpression2 = Expression.Parameter(typeof(object), propertyName);
MemberExpression propertyGetterExpression = Expression.Property(paramExpression, propertyName);
Action<TObject, object> result = Expression.Lambda<Action<TObject, object>>
(
Expression.Assign(propertyGetterExpression, paramExpression2), paramExpression, paramExpression2
).Compile();
return result;
}
and consume as simple as this.
var setter = GetPropSetter<TResponse>(property);
setter(response, deserializedResponse);
Aucun commentaire:
Enregistrer un commentaire