Cannot get Symbol for Alias part of UsingSyntax
-
2012年2月11日 7:10
I expected the following unit test to return a Symbol for System.Object from the alias foo I created. I actually get null. Am I not passing the correct SyntaxNode into GetDeclaredSymbol?
[TestMethod] public void UsingTest() { var compilation = Compilation.Create("foo.dll", references: new[] { MetadataReference.Create(typeof(object).Assembly.Location), }, syntaxTrees: new[] { SyntaxTree.ParseCompilationUnit( "using foo = System.Object;" ) } ); var tree = compilation.SyntaxTrees.Single(); var cu = (CompilationUnitSyntax)tree.Root; var usingSyntax = cu.Usings.Single(); var alias = usingSyntax.AliasOpt; var semanticModel = compilation.GetSemanticModel(tree); var info = semanticModel.GetDeclaredSymbol(alias); Assert.IsNotNull(info); }Thanks,
Chris
すべての返信
-
2012年2月11日 14:38
If you look at the overloads of GetDeclaredSymbol(), you'll notice there is one for UsingDirectiveSyntax. So I think you need to pass in the whole UsingDirectiveSyntax. The following code works for me:
var info = semanticModel.GetDeclaredSymbol(usingSyntax);
On the other hand, the documentation for the overload that you're using says:
A syntax node that is a declaration. This can be any type derived from MemberDeclarationSyntax, TypeDeclarationSyntax, EnumDeclarationSyntax, NamespaceDeclarationSyntax, ParameterSyntax, TypeParameterSyntax, or the alias part of a UsingDirectiveSyntax.
I think you are trying to use “the alias part of a UsingDirectiveSyntax”, so this might be a bug.
- 編集済み svick 2012年2月11日 14:45
-
2012年2月11日 20:35
Svick, yeah, the bug might be in the documentation. Although I did expect for the alias portion and the identifier portion to be resolveable to a symbol.
Thanks,
Chris -
2012年2月13日 21:01所有者
svick is correct - GetDeclaredSymbol() expects a UsingDirectiveSyntax instead of just the alias part. The doc comment text looks like a bug to me.
When you pass just the alias part (i.e. 'NameEqualsSyntax') of the using directive, this results in calling a generic overload of GetDeclaredSymbol() (i.e. an overload that accepts any 'SyntaxNode'). On the other hand, when you pass the using directive this results in calling a more specific overload of GetDeclaredSymbol (i.e. an overload that accepts exactly a 'UsingDirectiveSyntax')...
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
- 回答としてマーク kingces95 2012年2月14日 21:13
-
2012年2月13日 21:39所有者
Also, in our recent design thinking, we are considering removing the generic overload of GetDeclaredSymbol() i.e. the one that accepts any 'SyntaxNode'. This would mean that we would only be able to call GetDeclaredSymbols() with syntax types that 'can' map to declared symbols. This should help make the API less ambiguous.
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team

