c# - Get Namespace from InvocationExpressionSyntax in Roslyn Analyzer -
i'm attempting create analyzer roslyn prevent use of asserts within given namespace (to ensure project design standard maintained).
i've been able point can verify if assert, unsure how namespace context.
public override void initialize(analysiscontext context) { context.registersyntaxnodeaction(analyzemethod, syntaxkind.invocationexpression); } private static void analyzemethod(syntaxnodeanalysiscontext context) { var expression = (invocationexpressionsyntax)context.node; var memberaccessexpression = expression.expression memberaccessexpressionsyntax; if (memberaccessexpression == null) return; var membersymbol = modelextensions.getsymbolinfo(context.semanticmodel, memberaccessexpression).symbol imethodsymbol; if (!membersymbol?.tostring().contains("assert") ?? true) return; //check if we're inside page namespace. //this assert, lets fail it. var diagnostic = diagnostic.create(rule, memberaccessexpression.getlocation(), memberaccessexpression.name); context.reportdiagnostic(diagnostic); }
when inspecting context
object itself, can see containingsymbol
object, contains containingnamespace
property, when try code against this, don't appear able access it.
whats easiest method of getting class namespace? i.e. want namespace of class assert
in, not namespace of assert
.
as bonus question - there decent documentation on of this?
make sure have pulled down latest packages via nuget.
however don't understand why able see @ runtime using debugger unable code against it.
Comments
Post a Comment