像参数一样使用lambda [英] use lambda like a parameter
本文介绍了像参数一样使用lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
早上好。
i我的代码在c#中有问题。
以下是正确的代码(我可以把var名称结束,所以有字符串varName =E01020003):
Good morning.
i have a problem with my code in c#.
this below is the right code(i can take the var name end so have string varName="E01020003") :
public void writeTest()
{
varName = getVarName(() => E01020003);
...
...
}
public string VarName<T>(Expression<Func<T>> expr)
{
var body = ((MemberExpression)expr.Body);
return body.Member.Name;
}
这两种方法在同一个文件中.cs
i想要这样:
FILE1:
these two methods are in the same file .cs
i would like to to this:
FILE1:
public void Write()
{
...
...
...
file2.Write(userCred, E01020018, () => E01020003, (int)Type.Info, "test");
}
FILE2:
public bool Write(UserCredentials userCredentials, string ErrorText, Expression<Func<T>> expr, int type, string infoText)
{
bool returnValue = true;
var body = ((MemberExpression)expr.Body);
string varName = body.Member.Name;
...
...
...
}
有可能吗?我有问题传递lambda像参数。
问题1在调用
is it possible? i have problem to pass a lambda like a parameter.
problem 1 in the call
file2.Write(userCred, E01020018, () => E01020003, (int)Type.Info, "test");
以及名为:
$ b的方法$ b
and in the method called:
Write(UserCredentials userCredentials, string ErrorText, Expression<Func<T>> expr, int type, string infoText)
Best问题
Best regards
推荐答案
您使用表达式< Func< T>>的通用定义expr
但您没有将您的方法写入
声明为通用。如果expr
应该始终返回一个字符串,那么只需将其更改为Expression< Func< string>> expr
,否则你必须将方法声明改为写< T>(..)
。
You use a generic definition forExpression<Func<T>> expr
but you didn't declare your methodWrite
as generic. Ifexpr
should always return a string then just change it toExpression<Func<string>> expr
, otherwise you have to change the method declaration toWrite<T>(..)
.
在这里看到我的解决方案谢谢大家帮助我
现在它可以工作:
无法序列化类型 [ ^ ]
see my solution here thank u all for help me
now it works:
can not serialize the type[^]
这篇关于像参数一样使用lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文