This project is read-only.
1
Vote

Throw Exception on SemanticModel.GetDeclaredSymbol(FieldDeclarationSyntax)

description

Currently when calling SemanticModel.GetDeclaredSymbol() with a FieldDeclarationSyntax, a null value is returned. As FieldDeclarationSyntax can never map to a symbol, would it not make more sense to throw an InvalidArgumentException with an explanatory message?

I now understand that I should be passing the VariableDeclarationSyntax of the FieldDeclarationSyntax to GetDeclaredSymbol(), but as an end-user this was initially confusing.

There are a few arguments that are never going to be a valid use-cases. It might be better to show these messages (that exist within the source code) directly to the user via an exception:

http://source.roslyn.codeplex.com/#Microsoft.CodeAnalysis.CSharp/Compilation/SyntaxTreeSemanticModel.cs#c0a22ed2c09bd784#references

comments

Pilchie wrote Jul 28, 2014 at 12:25 AM

We generally have a policy that you can throw anything at GetDeclaredSymbol, GetSymbolInfo, GetTypeInfo, etch and see what results you get without having to worry about something being thrown. This particular case does tend to trip people up though,

JoshVarty wrote Sep 3, 2014 at 6:09 PM

I think I'm starting to come around to your side on this one.

Perhaps this can be remedied by noting that GlobalStatementSyntax, IncompleteMemberSyntax and BaseFieldDeclarationSyntax don't declare any symbols within the XML documentation for GetDeclaredSymbol().