为什么 roslyn SyntaxGenerator 不生成 params 数组和可选参数?
Why are params arrays and optional parameters not generated by the roslyn SyntaxGenerator?
roslyn SyntaxGenerator 基于符号生成语法。到目前为止,我已经相当广泛地使用了这些 API,没有出现任何问题,但最近我遇到了一个没有按预期工作的特定案例:
- 使用
SyntaxGenerator.MethodDeclaration(IMethodSymbol)
时,某些参数属性 未 在语法节点中正确生成,尽管它们存在于原始符号中。到目前为止,情况如下:
- params arrays(=> 相反,创建了一个普通的数组参数)
- 可选参数(=>缺少初始化表达式)
- 对于生成的 C# 代码和 VB.NET 都是如此(因此无论输出语言如何,params 数组和可选参数都不会正确生成)。
示例代码:
public class GenerationDemo
{
public static string GenerateMethod()
{
using var workspace = new AdhocWorkspace();
var project = GetProject(workspace);
var type = project.GetCompilationAsync().Result?.GetTypeByMetadataName(typeof(GenerationDemo).FullName);
var method = type.GetMembers(nameof(Test)).Cast<IMethodSymbol>().Single();
var generator = SyntaxGenerator.GetGenerator(project);
return generator.MethodDeclaration(method).NormalizeWhitespace().ToFullString();
}
public void Test(bool something = true, params int[] numbers)
{
}
private static Project GetProject(AdhocWorkspace workspace)
{
const string projectName = "TestProject";
var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), projectName,
projectName, LanguageNames.CSharp,
metadataReferences: new[]
{
MetadataReference.CreateFromFile(typeof(GenerationDemo).Assembly.Location),
MetadataReference.CreateFromFile(typeof(string).Assembly.Location)
});
return workspace.AddProject(projectInfo);
}
}
预期行为:
GenerationDemo.GenerateMethod()
returns:
public void Test(global::System.Boolean something = false, params global::System.Int32[] numbers)
{
}
实际行为:
GenerationDemo.GenerateMethod()
returns:
public void Test(global::System.Boolean something, global::System.Int32[] numbers)
{
}
问题:
- 为什么 roslyn SyntaxGenerator 不生成参数数组和可选参数等特定部分?
- 有没有办法在 SyntaxGenerator 的帮助下达到预期的结果(除了之后针对特定情况手动调整生成的语法)
- ...或者这只是 SyntaxGenerator 不支持?如果是这样,这是否记录在某处,还有什么没有生成?
这可能只是一个错误。如果您还没有,我建议您在 GitHub 上提交一个。
roslyn SyntaxGenerator 基于符号生成语法。到目前为止,我已经相当广泛地使用了这些 API,没有出现任何问题,但最近我遇到了一个没有按预期工作的特定案例:
- 使用
SyntaxGenerator.MethodDeclaration(IMethodSymbol)
时,某些参数属性 未 在语法节点中正确生成,尽管它们存在于原始符号中。到目前为止,情况如下:- params arrays(=> 相反,创建了一个普通的数组参数)
- 可选参数(=>缺少初始化表达式)
- 对于生成的 C# 代码和 VB.NET 都是如此(因此无论输出语言如何,params 数组和可选参数都不会正确生成)。
示例代码:
public class GenerationDemo
{
public static string GenerateMethod()
{
using var workspace = new AdhocWorkspace();
var project = GetProject(workspace);
var type = project.GetCompilationAsync().Result?.GetTypeByMetadataName(typeof(GenerationDemo).FullName);
var method = type.GetMembers(nameof(Test)).Cast<IMethodSymbol>().Single();
var generator = SyntaxGenerator.GetGenerator(project);
return generator.MethodDeclaration(method).NormalizeWhitespace().ToFullString();
}
public void Test(bool something = true, params int[] numbers)
{
}
private static Project GetProject(AdhocWorkspace workspace)
{
const string projectName = "TestProject";
var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), projectName,
projectName, LanguageNames.CSharp,
metadataReferences: new[]
{
MetadataReference.CreateFromFile(typeof(GenerationDemo).Assembly.Location),
MetadataReference.CreateFromFile(typeof(string).Assembly.Location)
});
return workspace.AddProject(projectInfo);
}
}
预期行为:
GenerationDemo.GenerateMethod()
returns:
public void Test(global::System.Boolean something = false, params global::System.Int32[] numbers)
{
}
实际行为:
GenerationDemo.GenerateMethod()
returns:
public void Test(global::System.Boolean something, global::System.Int32[] numbers)
{
}
问题:
- 为什么 roslyn SyntaxGenerator 不生成参数数组和可选参数等特定部分?
- 有没有办法在 SyntaxGenerator 的帮助下达到预期的结果(除了之后针对特定情况手动调整生成的语法)
- ...或者这只是 SyntaxGenerator 不支持?如果是这样,这是否记录在某处,还有什么没有生成?
这可能只是一个错误。如果您还没有,我建议您在 GitHub 上提交一个。