Repository navigation
Description
Description
Hello,
I think I found a bug in CSharp's CodeDom Provider.
I created a CodeDom statement that tries to invoke a method on a long string.
For some strings that meet the criteria below, the generator fails to generate a valid code.
I am getting an invalid result for a string that has a length:
- between 80 and 256 chars
- above 1500 chars
I think the problem is how the Microsoft.CSharp.CSharpCodeProvider.QuoteSnippetStringCStyle() method works. It does not consider the context of the generation process and splits strings into multiple lines.
Reproduction Steps
I prepared a sample code that reproduces a problem:
using System.CodeDom;
using System.CodeDom.Compiler;
var statement = new CodeAssignStatement(
new CodeFieldReferenceExpression
{
FieldName = "Value",
TargetObject = new CodeTypeReferenceExpression("PF")
},
new CodeMethodInvokeExpression
{
Parameters =
{
new CodePrimitiveExpression('|')
},
Method = new CodeMethodReferenceExpression
{
MethodName = "MethodName",
//TargetObject = new CodePrimitiveExpression(Sample_StringBelow80Chars()), // ok
TargetObject = new CodePrimitiveExpression(Sample_StringAbove80Chars()), // invalid code
//TargetObject = new CodePrimitiveExpression(Sample_StringBelow256Chars()), // invalid code
//TargetObject = new CodePrimitiveExpression(Sample_StringAbove256Chars()), // ok
//TargetObject = new CodePrimitiveExpression(Sample_StringAbove1500Chars()), // invalid code
}
}
);
var codeProvider = CodeDomProvider.CreateProvider("CSharp");
using TextWriter m = new StringWriter();
codeProvider.GenerateCodeFromStatement(statement, m, new CodeGeneratorOptions());
Console.WriteLine(m.ToString());
string Sample_StringBelow80Chars() => new string('*', 80);
// result:
// PF.Value = "********************************************************************************".MethodName('|');
string Sample_StringAbove80Chars() => new string('*', 82);
// result:
// PF.Value = "*********************************************************************************" +
// "*".MethodName('|');
string Sample_StringBelow256Chars() => new string('*', 170);
// result:
// PF.Value = "*********************************************************************************" +
// "********************************************************************************" +
// "*********".MethodName('|');
string Sample_StringAbove256Chars() => new string('*', 256);
// result:
// PF.Value = @"****************************************************************************************************************************************************************************************************************************************************************".MethodName('|');
string Sample_StringAbove1500Chars() => new string('*', 1501);
// result: invalidExpected behavior
The method should be invoked on the whole string.
Actual behavior
For some cases (described above), the method is invoked only on the last part of the string.
Also, if the method happens to return something that cannot be converted to a string, it will fail the compilation process of the generated code.
Regression?
I tested it on CodeDom library versions:
- 4.0.0 (.NET Framework)
- 4.4.0 (.NET 7)
- 7.0.0 (.NET 7)
- 8.0.0-preview (.NET 7)
It fails to generate valid code for every version.
Known Workarounds
If you generate code that wraps a string, like Convert.ToString() method invocation, it works fine.
Configuration
- NET 7
- Windows OS, Visual Studio IDE
- x64
Other information
No response
Activity
steveharter commentedon Aug 25, 2023
Verified the repro - string concat using
+when it shouldn't be doing that.