0

I am trying to learn how to create a Roslyn diagnostic analyzer and decided to try to develop it against a library I recently created; specifically, for one of the attributes. The attribute is called ValidateWith and can either take a System.Type or a string. When the attribute's argument is string, I am trying to confirm the following conditions are met:

  1. The string is the name of a method which exists in the same containing type
  2. The method accepts a single parameter which matches the property type (the attribute can only be applied to properties)
  3. The return type of the method is bool, Task<bool>, CustomValidationResult, or Task<CustomValidationResult>

I have been able to implement the vast majority of this but stuck on the third item. Something regarding checking the type of CustomValidationResult. Both CustomValidationResult class and ValidateWith attribute exists in the same, external, library.

For the comparison, I was able to find a helper method:

static bool TypeSymbolMatchesType(ITypeSymbol typeSymbol, Type type, SemanticModel semanticModel)
{
    return GetTypeSymbolForType(type, semanticModel).Equals(typeSymbol);
}

static INamedTypeSymbol GetTypeSymbolForType(Type type, SemanticModel semanticModel)
{

    if (!type.IsConstructedGenericType)
    {
        return semanticModel.Compilation.GetTypeByMetadataName(type.FullName);
    }

    // get all typeInfo's for the Type arguments 
    var typeArgumentsTypeInfos = type.GenericTypeArguments.Select(a => GetTypeSymbolForType(a, semanticModel));

    var openType = type.GetGenericTypeDefinition();
    var typeSymbol = semanticModel.Compilation.GetTypeByMetadataName(openType.FullName);
    return typeSymbol.Construct(typeArgumentsTypeInfos.ToArray<ITypeSymbol>());
}

(see: https://stackoverflow.com/a/33994426/10590099)

And this work for the bool and Task<bool> but when I try to get the type information for CustomValidationResult, the analyzer appears to not even run. The relevant code is as follow:

        private void AnalyzePropertySyntax(SyntaxNodeAnalysisContext context)
        {
            var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;

            context.ReportDiagnostic(Diagnostic.Create(Feedback, context.Node.GetLocation(), $"Total In List: {propertyDeclaration.AttributeLists.Count}"));

            try
            {
                ...
                ...
                
                context.ReportDiagnostic(Diagnostic.Create(Feedback, context.Node.GetLocation(), "Matching Return..."));

                var returnType = methodSymbol.ReturnType;
                var matchBool = TypeSymbolMatchesType(returnType, typeof(bool), context.SemanticModel);
                var matchBoolTask = TypeSymbolMatchesType(returnType, typeof(Task<bool>), context.SemanticModel);

                var type = typeof(object);
                throw new InvalidOperationException($"Type Info: {type}");
            }
            catch (Exception ex)
            {
                context.ReportDiagnostic(Diagnostic.Create(Exception, context.Node.GetLocation(), ex.ToString()));
            }
        }

In my testing, the above does work and I see an "error" that says Total In List: 1, which is what I expected. However, if I change var type = typeof(object); to var type = typeof(CustomValidationResult);, everything still builds/runs but I don't see anything in the Error list.

I would expect to see the "Total In List..." message since that should always be output. In addition, if there was an exception (which there should be since I am trying to throw one) but no exception is ever reported.

This is the first time I am trying to create a diagnostic analyzer, so I I am sure I am missing something simple, but have been stuck on this for a few hours. Any helper/guidance would be greatly appreciated. TIA.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.