Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:56:57+00:00 2026-05-23T21:56:57+00:00

I am attempting to create a Console.WriteLine statement (shown below) using CodeDom. This is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:56:58+00:00Added an answer on May 23, 2026 at 9:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the example console app below, you will notice that the program is attempting
I'm attempting to create a custom calendar control that inherits from ASP.Net's built in
I'm currently attempting to create a tabbed interface in a web application, and based
I am attempting to create a small application to collect data received from an
I am attempting to create new membership users in an Ektron CMS400.NET-based website by
I'm attempting to create my first real C# application - a small pet project
I'm attempting to load a dll into MSSQL with: USE dbname GO CREATE ASSEMBLY
I'm attempting to authenticate a user against ADAM using a user I created in
the server throws this error Attempting to deserialize an empty stream at server side
I'm using super-simple synchronous C# Sockets for an assignment. The assignment is to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.