C# set property value of property passed to method via Func<T>

.net, c#, func

Solution

Youd could change the `Encrypt` function like this:

public static void Encrypt<T>(Action<T> prop, T value, string username, string password)
{
  // awesome stuff before
  prop(value);
  // awesome stuff after
}

Then call `Encrypt` :

Encrypt(value => obj.Prop = value, 23, "", "");

Problem

I use this method to get the value of a property in a method: ``` public static T Decrypt<T>(Func<T> prop, string username, string password) { T value = prop(); //do cool stuff with t return value; } ``` I'm looking for a way to do the other way arround, set the value of my property ``` public static void Encrypt<T>(Func<T> prop, T value, string username, string password) { //do stuff with value ??? set prop ??? } ``` I've searched and tried Expressions, but cloud not get it to work: ``` public static void Encrypt<T>(Expression<Func<T>> property, T value, string username, string password) { //do stuff with value var propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo; propertyInfo.SetValue(property, value); } ```

Original source

Related problems