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?