This project is read-only.

Throw Exception on SemanticModel.GetDeclaredSymbol(FieldDeclarationSyntax)


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:


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().