转换表达式< func< t,object>>到字符串
我尝试以expression< func< persock>>
将表达式转换为以后操作的字符串。
此表达式存储在类的属性中,并用于源代码生成。
示例1(这是按预期起作用的)
Expression<Func<Person,object>> exp1 = p => "abc";
Console.WriteLine(exp1.ToString() ); // ok, p => "abc"
示例2(这具有预期的结果):
Expression<Func<Person,object>> exp2 = p => Guid.NewGuid();
Console.WriteLine(exp2.ToString());
//the string is including a convert method: p => Convert(NewGuid(), Object)
我希望表达式为p =&gt; guid.newguid()
,不包括convert
方法:f =&gt; convert(newguid(),对象)
转换
方法中的问题是字符串表达式不可用,并且在生成的新源代码中导致汇编错误。
我需要做什么才能按照:f =&gt; Guid.NewGuid()
没有转换方法?
a
I try to convert an expression in the form Expression<Func<Person,object>>
to a string for later manipulation.
This expression is stored in a property in a class and it's used in source code generation.
Example 1 (This works as expected)
Expression<Func<Person,object>> exp1 = p => "abc";
Console.WriteLine(exp1.ToString() ); // ok, p => "abc"
Example 2 (This has a result that is not expected):
Expression<Func<Person,object>> exp2 = p => Guid.NewGuid();
Console.WriteLine(exp2.ToString());
//the string is including a convert method: p => Convert(NewGuid(), Object)
I expect expression to be p => Guid.NewGuid()
, without including the Convert
method: f=> Convert(NewGuid(), Object)
The problem in the Convert
method is the string expression is un usable and cause a compilation error in the new source code generated.
What do I need to do to get the expression as is: f => Guid.NewGuid()
without a Convert method?
Demo for a test case
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用仿制药,您可能会得到所需的东西。
这是一个片段。
小提琴,以便您可以进行更多的测试。
By using Generics you might get what you're looking for.
Here's a snippet.
A fiddle so you can do some more testing.