C# CodeDom Console.WriteLine +方法参考

发布于 2024-11-24 23:27:16 字数 1180 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城追梦 2024-12-01 23:27:16

您必须像正常编写代码一样编写 CodeDOM。也就是说,当你想用一个参数调用一个方法时,你必须给它一个参数。

另外,如果您想在生成的代码中使用常量,正确的方法是使用CodePrimitiveExpression。您正在尝试创建名为“从源读取时出错:” 的变量。

因为手动构造整个表达式会很乏味且不可读,所以您可以创建一个辅助方法,用于使用相同的运算符连接多个表达式:

static CodeExpression Join(
    CodeBinaryOperatorType op, params CodeExpression[] expressions)
{
    return expressions.Aggregate((l, r) => new CodeBinaryOperatorExpression(l, op, r));
}

使用该方法,您现在可以编写:

new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression(typeof(Console)), "WriteLine",
    Join(CodeBinaryOperatorType.Add,
            new CodePrimitiveExpression("Error reading from source: "),
            new CodePropertyReferenceExpression(
                new CodeTypeReferenceExpression(typeof(Environment)),
                "NewLine"),
            new CodePrimitiveExpression("Error code: "),
            new CodePropertyReferenceExpression(
                new CodeVariableReferenceExpression("ex"), "Message")));

生成以下代码:

System.Console.WriteLine(((("Error reading from source: " + System.Environment.NewLine)
                + "Error code: ")
                + ex.Message))

当然,您始终可以使用片段:

new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression(typeof(Console)), "WriteLine",
    new CodeSnippetExpression(
        "\"Error reading from source: \" + Environment.NewLine + \"Error code: \" + ex.Message"));

此外,使用 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:

static CodeExpression Join(
    CodeBinaryOperatorType op, params CodeExpression[] expressions)
{
    return expressions.Aggregate((l, r) => new CodeBinaryOperatorExpression(l, op, r));
}

Using that, you can now write:

new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression(typeof(Console)), "WriteLine",
    Join(CodeBinaryOperatorType.Add,
            new CodePrimitiveExpression("Error reading from source: "),
            new CodePropertyReferenceExpression(
                new CodeTypeReferenceExpression(typeof(Environment)),
                "NewLine"),
            new CodePrimitiveExpression("Error code: "),
            new CodePropertyReferenceExpression(
                new CodeVariableReferenceExpression("ex"), "Message")));

Which produces the following code:

System.Console.WriteLine(((("Error reading from source: " + System.Environment.NewLine)
                + "Error code: ")
                + ex.Message))

Of course, you can always use snippets:

new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression(typeof(Console)), "WriteLine",
    new CodeSnippetExpression(
        "\"Error reading from source: \" + Environment.NewLine + \"Error code: \" + ex.Message"));

Also, using the formatting overload of Console.WriteLine() might be better.

静若繁花 2024-12-01 23:27:16

使用 Console.WriteLine(String, Object[] ) 重载并执行与

Console.WriteLine("Error reading from source: {0} Error code: {1}", 
    Environment.NewLine, 
    ex.Message);

CodeDom 中相同的操作。

Use the Console.WriteLine(String, Object[]) overload and do the equivalent to

Console.WriteLine("Error reading from source: {0} Error code: {1}", 
    Environment.NewLine, 
    ex.Message);

in CodeDom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文