3

Does Roslyn CTP support CallerMemberNameAttribute and others similar?

I have a method with several parameters, some obligatory, and also there is a parameter with default value, marked with CallerMemberName attribute.

I am generating a call of this method by Roslyn, passing only obligatory parameters, and expecting it to generate values for compiler-generated parameters too, but this does not happen, they have default values.

Is this a limitation of Roslyn's current version, or I am doing something wrong?

4

1 回答 1

6

Microsoft“Roslyn”2012 年 9 月 CTP 中的新增功能中,指出:

解析器支持完整的 C# 4 和 Visual Basic 10语言,但有几个语言功能尚未在当前的 Roslyn 编译器中完全实现。

调用方信息功能是在 .NET Framework 4.5(C# 5.0 和 Visual Basic 11)中引入的。因此,Roslyn 在当前版本中不支持它。然而,获取调用者信息可以使用类似以下代码片段的内容来完成,来自“我可以从 Roslyn 获取函数调用者/被调用者信息吗? ”在 MSDN 论坛中提出的问题:

var syntaxTree = SyntaxTree.ParseCompilationUnit(code);

var semanticModel = Compilation.Create("compilation")
    .AddSyntaxTrees(syntaxTree)
    .AddReferences(new AssemblyFileReference(typeof(object).Assembly.Location))
    .GetSemanticModel(syntaxTree);

var baz = syntaxTree.Root
    .DescendentNodes()
    .OfType<ClassDeclarationSyntax>()
    .Single(m => m.Identifier.ValueText == "C1")
    .ChildNodes()
    .OfType<MethodDeclarationSyntax>()
    .Single(m => m.Identifier.ValueText == "Baz");

var bazSymbol = semanticModel.GetDeclaredSymbol(baz);

var invocations = syntaxTree.Root
    .DescendentNodes()
    .OfType<InvocationExpressionSyntax>();

var bazInvocations = invocations
    .Where(i => semanticModel.GetSemanticInfo(i).Symbol == bazSymbol);
于 2013-06-24T08:49:22.180 回答