I have created a diagnostic analyzer which reports various issues with custom attributes for properties. However, I am having trouble get the location of only the attribute and/or attribute arguments (depending on the specific diagnostic being reported).
As there are (currently) two attributes that need to be analyzed, I created a helper method which determines if the attribute exists on the property:
private static bool HasAttribute(SyntaxNodeAnalysisContext context, string attributeName, out PropertyAttributeData data)
{
var propertyDeclarationSyntax = (PropertyDeclarationSyntax)context.Node;
data = new PropertyAttributeData
{
Type = propertyDeclarationSyntax.Type
};
var propertyAttribute = propertyDeclarationSyntax.AttributeLists.FirstOrDefault()
?.GetAttributes(context.Compilation)
?.FirstOrDefault(attr => attr.AttributeClass?.Name == attributeName);
if (propertyAttribute == null)
{
return false;
}
data.AttributeData = propertyAttribute;
data.ConstructorArg1 = propertyAttribute.ConstructorArguments.FirstOrDefault();
if (propertyAttribute.ConstructorArguments.Length > 1)
{
data.ConstructorArg2 = propertyAttribute.ConstructorArguments.Skip(1).FirstOrDefault();
}
return true;
}
However, this is where I kind of get stuck. I have been using context.Node.GetLocation
for testing purposes, but this underlines the attribute and property. I have also tried using the SemanticModel.GetSymbolInfo()
and SemanticModel.GetDeclaredSymbol()
, as well as a few other things. However, the location I get back is either from the start of the attribute to the end of the file or it is reporting the original location of the attribute (in a separate library).
How would I go about getting the location of just the attribute or the first/second argument of the it? Any guidance would be appreciated. TIA.
I have created a diagnostic analyzer which reports various issues with custom attributes for properties. However, I am having trouble get the location of only the attribute and/or attribute arguments (depending on the specific diagnostic being reported).
As there are (currently) two attributes that need to be analyzed, I created a helper method which determines if the attribute exists on the property:
private static bool HasAttribute(SyntaxNodeAnalysisContext context, string attributeName, out PropertyAttributeData data)
{
var propertyDeclarationSyntax = (PropertyDeclarationSyntax)context.Node;
data = new PropertyAttributeData
{
Type = propertyDeclarationSyntax.Type
};
var propertyAttribute = propertyDeclarationSyntax.AttributeLists.FirstOrDefault()
?.GetAttributes(context.Compilation)
?.FirstOrDefault(attr => attr.AttributeClass?.Name == attributeName);
if (propertyAttribute == null)
{
return false;
}
data.AttributeData = propertyAttribute;
data.ConstructorArg1 = propertyAttribute.ConstructorArguments.FirstOrDefault();
if (propertyAttribute.ConstructorArguments.Length > 1)
{
data.ConstructorArg2 = propertyAttribute.ConstructorArguments.Skip(1).FirstOrDefault();
}
return true;
}
However, this is where I kind of get stuck. I have been using context.Node.GetLocation
for testing purposes, but this underlines the attribute and property. I have also tried using the SemanticModel.GetSymbolInfo()
and SemanticModel.GetDeclaredSymbol()
, as well as a few other things. However, the location I get back is either from the start of the attribute to the end of the file or it is reporting the original location of the attribute (in a separate library).
How would I go about getting the location of just the attribute or the first/second argument of the it? Any guidance would be appreciated. TIA.
Share Improve this question asked Jan 20 at 19:24 DominickDominick 4761 gold badge7 silver badges21 bronze badges1 Answer
Reset to default 1If you have tried (AttributeSyntax)attribute.GetLocation()
then try attribute.Name.GetLocation()
or for argument (AttributeArgumentSyntax)argument.Expression.GetLocation()
. Pretty sure that is what worked for me.