C# CodeDom Console.WriteLine +方法参考
我正在尝试使用 CodeDom 创建一个 Console.WriteLine
语句(如下所示)。这主要是因为 Environment.NewLine
调用 - 我无法找出在 Console.WriteLine 调用中嵌入方法的正确方法。我将向您展示我正在尝试生成的代码以及我正在使用的代码。也许有人能够发现并纠正我的错误。
我想要生成的代码:
Console.WriteLine("Error reading from source: " + Environment.NewLine + "Error code: " + ex.Message);
我正在使用的代码:
const char quote = '\u0022'; // Represents a " mark
CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("Console"), "WriteLine",
new CodeExpression[] {
new CodeVariableReferenceExpression(quote + "Error reading from source: " + quote + " + "),
new CodeFieldReferenceExpression("Environment"), "NewLine"),
new CodeVariableReferenceExpression("+ " + quote + "Error code: " + quote + " + " + "ex" + ".Message")})));
正在生成的代码:
Console.WriteLine("Error reading source: " + , Environment.NewLine, + "Error code: " + ex.Message);
因为我正在使用 MethodInvoke
,CodeDom 用“,”分隔每一行,就好像它们是同一方法中的每个新参数一样......我该如何解决这个问题正确的方法?
I am attempting to create a Console.WriteLine
statement (shown below) using CodeDom. This is giving me mainly because of the Environment.NewLine
call - I can't figure out the proper way of embedding a method within the Console.WriteLine call. I will show you the code I am trying to produce along with the code that I am using. Perhaps someone will be able to catch, and correct my error.
The code I would like to produce:
Console.WriteLine("Error reading from source: " + Environment.NewLine + "Error code: " + ex.Message);
The code that I am using:
const char quote = '\u0022'; // Represents a " mark
CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("Console"), "WriteLine",
new CodeExpression[] {
new CodeVariableReferenceExpression(quote + "Error reading from source: " + quote + " + "),
new CodeFieldReferenceExpression("Environment"), "NewLine"),
new CodeVariableReferenceExpression("+ " + quote + "Error code: " + quote + " + " + "ex" + ".Message")})));
The code that is being generated:
Console.WriteLine("Error reading source: " + , Environment.NewLine, + "Error code: " + ex.Message);
Because I am using MethodInvoke
, CodeDom is separating each line by a ", " as if they were each new parameters within the same method... How can I go about this the proper way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须像正常编写代码一样编写 CodeDOM。也就是说,当你想用一个参数调用一个方法时,你必须给它一个参数。
另外,如果您想在生成的代码中使用常量,正确的方法是使用
CodePrimitiveExpression
。您正在尝试创建名为“从源读取时出错:”
的变量。因为手动构造整个表达式会很乏味且不可读,所以您可以创建一个辅助方法,用于使用相同的运算符连接多个表达式:
使用该方法,您现在可以编写:
生成以下代码:
当然,您始终可以使用片段:
此外,使用
Console.WriteLine()
的格式化重载可能会更好。You have to write the CodeDOM the same way you write the code normally. That is, when you want to call a method with one argument, you have to give it one argument.
Also, if you want to have a constant in the generated code, the proper way is to use
CodePrimitiveExpression
. You are trying to creating a variable with the name"Error reading from source: "
.Because constructing the whole expression by hand would be tedious and unreadable, you could create a helper method for joining multiple expressions using the same operator:
Using that, you can now write:
Which produces the following code:
Of course, you can always use snippets:
Also, using the formatting overload of
Console.WriteLine()
might be better.使用 Console.WriteLine(String, Object[] ) 重载并执行与
CodeDom 中相同的操作。
Use the Console.WriteLine(String, Object[]) overload and do the equivalent to
in CodeDom.