Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#设置通过Func传递给方法的属性的属性值<;T>;_C#_.net_Func - Fatal编程技术网

C#设置通过Func传递给方法的属性的属性值<;T>;

C#设置通过Func传递给方法的属性的属性值<;T>;,c#,.net,func,C#,.net,Func,我使用此方法获取方法中属性的值: public static T Decrypt<T>(Func<T> prop, string username, string password) { T value = prop(); //do cool stuff with t return value; } publicstatict解密(Func属性、字符串用户名、字符串密码) { T值=prop(); //用t做一些很酷的事情 返回值; } 我正在

我使用此方法获取方法中属性的值:

public static T Decrypt<T>(Func<T> prop, string username, string password)
{
    T value = prop();
    //do cool stuff with t
    return value;
}
publicstatict解密(Func属性、字符串用户名、字符串密码)
{
T值=prop();
//用t做一些很酷的事情
返回值;
}
我正在寻找另一种方法,设置我财产的价值

public static void Encrypt<T>(Func<T> prop, T value, string username, string password)
{
    //do stuff with value
    ??? set prop ???
}
publicstaticvoidencrypt(Func属性、T值、字符串用户名、字符串密码)
{
//做有价值的事
设置道具???
}
我已经搜索并尝试了表达式,但没有成功:

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);
}
publicstaticvoidencrypt(表达式属性、T值、字符串用户名、字符串密码)
{
//做有价值的事
var propertyInfo=((MemberExpression)property.Body).propertyInfo作为成员;
propertyInfo.SetValue(属性,值);
}

您对SetValue方法的行为存在误解,它将实际对象作为第一个参数,该参数具有描述propertyInfo的属性,因此您需要采用该对象形式的表达式,而不是使用表达式本身,请查看下面关于stakoverflow的回答,这可能会对您有所帮助

您可以更改
加密功能,如下所示:

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

您可以尝试按名称设置属性:

Type t = ctrl.GetType();
t.InvokeMember(propName, BindingFlags.Instance | BindingFlags.SetProperty |BindingFlags.Public, null, ctrl, new object[] { value });

你能用
set
场景发布通话代码吗?换句话说:如何调用
Decrypt
?加密方法必须使用
Func
委托,还是可以使用其他委托?@Dirk否我可以使用其他委托。然后,我建议使用与@WolfgangZiegler相同的解决方案
Type t = ctrl.GetType();
t.InvokeMember(propName, BindingFlags.Instance | BindingFlags.SetProperty |BindingFlags.Public, null, ctrl, new object[] { value });