转换 lambda 表达式
我有以下代码
Expression<Func<IPersistentAttributeInfo, bool>> expression = info => info.Owner== null;
,并希望将其转换为
Expression<Func<PersistentAttributeInfo, bool>> expression = info => info.Owner== null;
仅在运行时才知道的 PersistentAttributeInfo,但这
可能吗?
I have the following code
Expression<Func<IPersistentAttributeInfo, bool>> expression = info => info.Owner== null;
and want to tranform it to
Expression<Func<PersistentAttributeInfo, bool>> expression = info => info.Owner== null;
PersistentAttributeInfo is only known at runtime though
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 PersistentAttributeInfo 仅在运行时已知,那么您显然无法静态编写 lambda 并让编译器为您完成繁重的工作。您必须从头开始创建一个新的委托:
您可以调用 lambda.Compile() 来返回一个委托,该委托类似于示例中转换后的 lambda 表达式(当然,它是无类型的)。
If PersistentAttributeInfo is only known at runtime, you obviously cannot write the lambda statically and have the compiler do the heavy lifting for you. You'll have to create a new one from scratch:
You can invoke lambda.Compile() to return a Delegate that is analogous to the transformed lambda expression in your example (though of course untyped).