Overloads of SemanticModel.GetDeclaredSymbol()
-
sexta-feira, 17 de fevereiro de 2012 22:10
When you call SemanticModel.GetDeclaredSymbol() with MethodDeclarationSyntax as a parameter, you get back a MethodSymbol. But there is no specific overload for MethodDeclarationSyntax, so the overload for MemberDeclarationSyntax is used instead.
As a consequence, the return type is statically typed as Symbol and to actually get MethodSymbol, one has to use cast, e.g.:
MethodDeclarationSyntax methodSyntax = …; MethodSymbol methodSymbol = (MethodSymbol)model.GetDeclaredSymbol(methodSyntax);
It would be nice if there was a overload specifically for MethodDeclarationSyntax that returns MethodSymbol, so that the cast wasn't necessary. And similarly for other types derived from MemberDeclarationSyntax.
I think the way it's currently is confusing, because there is no straightforward way to find out that you should cast to MethodSymbol.
Do you think doing this would be a good idea? Is there some case where GetDeclaredSymbol() for MethodDeclarationSyntax returns something else than MethodSymbol?
One reason why doing this would be bad is that it would add more overloads to a method that already has lots of them. But I think benefits outweigh that issue.
Todas as Respostas
-
sexta-feira, 17 de fevereiro de 2012 22:28Proprietário
Thanks svick! I think thats good feedback. I agree its cumbersome and sometimes confusing to have to cast stuff around. I think there may also be other Roslyn APIs in the Syntax and Symbols space that suffer from similar casting issues (since the inheritance hierarchy for some SyntaxNodes / Symbols can be 'wide').
Based on your previous response on this thread, I have logged this suggestion internally on your behalf :) However, please feel free to log through Connect if you wish and I can close the internal bug that I opened as a dupe of your bug.
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
- Editado Shyam NamboodiripadMicrosoft Employee, Owner sexta-feira, 17 de fevereiro de 2012 22:33
-
quarta-feira, 18 de abril de 2012 21:51Proprietário
Hey svick,
The Roslyn design team looked at the issue you reported and were surprised that a strongly-typed overload for MethodDeclarationSyntax didn't exist already. It was certainly our intention that there would be one and that you wouldn't need to cast. In current builds there is an overload which takes BaseMethodSyntax which is the base class of Method, Constructor, Destructor, and OperatorDeclarationSyntax and returns a MethodSymbol for these.
There aren't any more strongly-typed overloads, however, since they aren't needed. Does that help your issue or would you prefer to have overloads for each concrete type anyway?
-ADG
Anthony D. Green | Program Manager | Visual Basic & C# Languages Team
-
quarta-feira, 18 de abril de 2012 21:56I think it makes sense the way you describe it. I don't see any reason for having overloads for each type, if they would all have the same return type.
- Editado svick quarta-feira, 18 de abril de 2012 21:56

