最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Roslyn Diagnostic Analzyer - Underline Only An Attribute Parameter - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

If 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.

发布评论

评论列表(0)

  1. 暂无评论