Roslyn Analyzer not clearing last diagnostic - c#

I have created a Roslyn Analyzer that validates text files against a set of rules and have noticed that when I fix the last issue that diagnostic does not disappear. I have created a simple example to replicate the problem below.
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class Analyzer1Analyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "TestCompilationAnalyzer";
private static readonly LocalizableString Title = "TestAnalyzer";
private static readonly LocalizableString MessageFormat = "This message should always appear {0}";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, "Test", DiagnosticSeverity.Warning, isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterAdditionalFileAction(AdditionalFileAction);
}
private void AdditionalFileAction(AdditionalFileAnalysisContext cxt)
{
var text = File.ReadAllText(cxt.AdditionalFile.Path);
if (text.Contains("help"))
{
cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"help"));
}
if (text.Contains("test"))
{
cxt.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, $"test"));
}
}
}
if I add the words help and test to my additional file both diagnostics appear but if I remove both whatever was showing last won't disappear.

I found a workaround using code generators instead
[Generator]
public class SchemaTestAnalyzer : ISourceGenerator
{
public const string DiagnosticId = "TestCompilationAnalyzer";
private static readonly LocalizableString Title = "TestAnalyzer";
private static readonly LocalizableString MessageFormat = "This message should always appear {0}";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, "Test", DiagnosticSeverity.Warning, isEnabledByDefault: true);
public void Execute(GeneratorExecutionContext context)
{
var additionalFile = context.AdditionalFiles.FirstOrDefault();
if (additionalFile != null)
{
var text = File.ReadAllText(additionalFile.Path);
if (text.Contains("help"))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, "help"));
}
if (text.Contains("test"))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, "test"));
}
}
}
public void Initialize(GeneratorInitializationContext context) { }
}
these work as expected and the last issue disappears when addressed and re-analyzed

Related

What's the best way to check source code lines for exceeding line length with Roslyn Analyzer?

I'd like to enforce some hard limits to code line length.
What Roslyn's API should I use for it?
Registering action for every syntax node and checking node's location seems to be not the most efficient approach.
An easy approach might be to register for syntax trees, but then immediately just call GetText() on the syntax tree; from there you can look at the lines of the text directly and at least find "long" lines since you can directly get line spans and lengths that way. You'll still potentially need to filter out things though like long string literals or something.
Not perfect solution:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class LineMaxLengthAnalyzer : DiagnosticAnalyzer
{
public const int LineMaxLength = 150;
public const string DiagnosticId = nameof(LineMaxLengthAnalyzer);
private static readonly LocalizableString Title = new LocalizableResourceString(
nameof(Resources.LineMaxLengthAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(
nameof(Resources.LineMaxLengthAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description = new LocalizableResourceString(
nameof(Resources.LineMaxLengthAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private const string Category = "Readability";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
var nodeTypes = Enum.GetValues(typeof(SyntaxKind)).Cast<SyntaxKind>().ToArray();
context.RegisterSyntaxTreeAction(VerifyLines);
}
private void VerifyLines(SyntaxTreeAnalysisContext context)
{
var text = context.Tree.GetText();
text.Lines.Where(line => line.End - line.Start + 1 > LineMaxLength).ToList()
.ForEach(line =>
{
var location = Location.Create(context.Tree, line.Span);
var diagnostic = Diagnostic.Create(Rule, location, LineMaxLength);
context.ReportDiagnostic(diagnostic);
});
}
}

How to deserialize readonly fields using C# Driver for MongoDB?

I have a class which contains readonly private fields
public sealed class Company : AggregateRoot
{
private readonly List<TimeSeriesImportInfo> _executedImportInfo = new();
private readonly List<StockPrice> _stockPrices = new();
public Company(
Guid id,
string name,
string symbol)
: base(id)
{
Name = name;
Symbol = symbol;
}
public string Name { get; }
public string Symbol { get; }
public IReadOnlyCollection<TimeSeriesImportInfo> ExecutedImportInfo => _executedImportInfo.AsReadOnly();
public IReadOnlyCollection<StockPrice> StockPrices => _stockPrices.AsReadOnly();
public void AddExecutedImportInfo(DateTime month)
{
var timeSeriesImportInfo = new TimeSeriesImportInfo(month);
var hasAlreadyBeenHandled = ExecutedImportInfo.Contains(timeSeriesImportInfo);
if (hasAlreadyBeenHandled)
{
throw new ImportExecutedBeforeDomainException(Id, month);
}
_executedImportInfo.Add(timeSeriesImportInfo);
var importExecutedEvent = new ImportExecutedDomainEvent(Id, timeSeriesImportInfo.Month);
Enqueue(importExecutedEvent);
}
public void AddStockPrices(ICollection<StockPrice> stockPrices)
{
var hasAlreadyBeenImported = stockPrices.Any(newStockPrice =>
_stockPrices.Contains(newStockPrice));
if (hasAlreadyBeenImported)
{
throw new StockPriceAlreadyImportedDomainException(Id, stockPrices);
}
_stockPrices.AddRange(stockPrices);
var stockPricesAddedEvent = new StockPricesAddedDomainEvent(Id, stockPrices);
Enqueue(stockPricesAddedEvent);
}
}
I have configured the mapping for the company using the following method:
public static class Mapping
{
public static IServiceCollection AddMappings(this IServiceCollection services)
{
BsonClassMap.RegisterClassMap<Company>(classMap =>
{
classMap.AutoMap();
classMap.MapField("_executedImportInfo").SetElementName(nameof(Company.ExecutedImportInfo));
classMap.MapField("_stockPrices").SetElementName(nameof(Company.StockPrices));
});
return services;
}
}
The values of those private readonly fields are correctly saved to the database. However when im trying to fetch company object - those private fields can't be deserialized properly, because after the fetch they have default values (although they are persisted in the db). If i removed the "readonly" keyword from them - everything works correctly. I would like to avoid removing the readonly key and would like to not modify the Company class. How to achieve that in C#? Thanks for any help, regards.
it's not supported, readonly fields are skipped during deserialization after ctor called.

How do I debug a CodeFixProvider not ever loading

I have made a new "Analyzer with Code Fix (.NET Standard)" project, and updated the analyzer to check for what I want, and that works great. Now I have modified the CodeFixProvider, but it never shows up when debugging.
I have set breakpoints in the getter for FixableDiagnosticIds, GetFixAllProvider(), and RegisterCodeFixesAsync(CodeFixContext context), yet none of the breakpoints ever get called when I click the "light bulb" on a line that the analyzer, properly, tags.
Any ideas on how to figure out why it doesn't seem to be getting called?
In the default project created by "Analyzer with Code Fix (.NET Standard)", breakpoints in those 3 places are getting called correctly.
My analyzer code
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace InAnalyzer
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class InAnalyzerAnalyzer : DiagnosticAnalyzer
{
public const string CanBeInvokedWithInDiagnosticId = "IN3001";
public const string DoNotUseInWithParameterDiagnosticId = "IN3002";
public const string UseInWithParameterDiagnosticId = "IN3003";
private const string CanBeInvokedWithInCategory = "Performance";
private const string DoNotUseInWithParameterCategory = "Performance";
private const string UseInWithParameterCategory = "Performance";
private static readonly LocalizableString CanBeInvokedWithInTitle = new LocalizableResourceString(nameof(Resources.CanBeInvokedWithInAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString DoNotUseInWithParameterTitle = new LocalizableResourceString( nameof(Resources.DoNotUseInWithParameterAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString UseInWithParameterTitle = new LocalizableResourceString( nameof(Resources.UseInWithParameterAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString CanBeInvokedWithInMessageFormat = new LocalizableResourceString(nameof(Resources.CanBeInvokedWithInAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString DoNotUseInWithParameterMessageFormat = new LocalizableResourceString(nameof(Resources.DoNotUseInWithParameterAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString UseInWithParameterMessageFormat = new LocalizableResourceString(nameof(Resources.UseInWithParameterAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString CanBeInvokedWithInDescription = new LocalizableResourceString(nameof(Resources.CanBeInvokedWithInAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString DoNotUseInWithParameterDescription = new LocalizableResourceString(nameof(Resources.DoNotUseInWithParameterAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString UseInWithParameterDescription = new LocalizableResourceString(nameof(Resources.UseInWithParameterAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private static readonly DiagnosticDescriptor CanBeInvokedWithInRule = new DiagnosticDescriptor(
CanBeInvokedWithInDiagnosticId,
CanBeInvokedWithInTitle,
CanBeInvokedWithInMessageFormat,
CanBeInvokedWithInCategory,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
CanBeInvokedWithInDescription);
private static readonly DiagnosticDescriptor DoNotUseInWithParameterRule = new DiagnosticDescriptor(
DoNotUseInWithParameterDiagnosticId,
DoNotUseInWithParameterTitle,
DoNotUseInWithParameterMessageFormat,
DoNotUseInWithParameterCategory,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
DoNotUseInWithParameterDescription);
private static readonly DiagnosticDescriptor UseInWithParameterRule = new DiagnosticDescriptor(
UseInWithParameterDiagnosticId,
UseInWithParameterTitle,
UseInWithParameterMessageFormat,
UseInWithParameterCategory,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
UseInWithParameterDescription);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
=> ImmutableArray.Create(CanBeInvokedWithInRule, DoNotUseInWithParameterRule, UseInWithParameterRule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeInvocationExpressionSyntaxNode, SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeAction(AnalyzeMethodDeclarationSyntaxNode, SyntaxKind.MethodDeclaration);
}
private static void AnalyzeInvocationExpressionSyntaxNode(SyntaxNodeAnalysisContext context)
{
var node = (InvocationExpressionSyntax) context.Node;
var symbol = context.SemanticModel.GetSymbolInfo(node).Symbol ??
context.SemanticModel.GetDeclaredSymbol(node);
if (symbol is IMethodSymbol methodSymbol)
{
var parametersSymbol = methodSymbol.Parameters;
var argumentSyntaxList = node?.ArgumentList.Arguments;
if (argumentSyntaxList != null)
{
var argumentSyntaxes = argumentSyntaxList.Value;
for (var index = 0; index < parametersSymbol.Length; index++)
{
var parameterSymbol = parametersSymbol[index];
if (parameterSymbol.RefKind == RefKind.In &&
parameterSymbol.Type.IsReadOnly &&
parameterSymbol.Type.IsValueType &&
index < argumentSyntaxes.Count)
{
var argumentSyntax = argumentSyntaxes[index];
if (argumentSyntax?.RefKindKeyword.IsKind(SyntaxKind.InKeyword) == false)
{
var diagnostic = Diagnostic.Create(
CanBeInvokedWithInRule,
argumentSyntax.Expression.GetLocation(),
parameterSymbol.Name,
parameterSymbol.Type);
context.ReportDiagnostic(diagnostic);
}
}
}
}
}
}
private static void AnalyzeMethodDeclarationSyntaxNode(SyntaxNodeAnalysisContext context)
{
var node = (MethodDeclarationSyntax) context.Node;
var parameterSyntaxList = node?.ParameterList.Parameters;
if (parameterSyntaxList != null)
{
var parameterSyntaxes = parameterSyntaxList.Value;
for (var index = 0; index < parameterSyntaxes.Count; index++)
{
var parameterSyntax = parameterSyntaxes[index];
if (parameterSyntax != null)
{
var symbol = context.SemanticModel.GetSymbolInfo(parameterSyntax.Type).Symbol;
if (symbol is ITypeSymbol typeSymbol)
{
if (typeSymbol.IsReadOnly &&
typeSymbol.IsValueType)
{
if (!parameterSyntax.Modifiers.Any(SyntaxKind.InKeyword))
{
var diagnostic = Diagnostic.Create(
UseInWithParameterRule,
parameterSyntax.Identifier.GetLocation(),
parameterSyntax.Identifier,
typeSymbol);
context.ReportDiagnostic(diagnostic);
}
}
else
{
foreach (var modifier in parameterSyntax.Modifiers)
{
if (modifier.Kind() == SyntaxKind.InKeyword)
{
var diagnostic = Diagnostic.Create(
DoNotUseInWithParameterRule,
modifier.GetLocation(),
parameterSyntax.Identifier,
typeSymbol);
context.ReportDiagnostic(diagnostic);
}
}
}
}
}
}
}
}
}
}
My CodeFixProvider (Probably not complete and correct; I'd like to debug and get it working correctly, but I can't get it to even try to run):
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
namespace InAnalyzer
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(InAnalyzerCodeFixProvider)), Shared]
public class InAnalyzerCodeFixProvider : CodeFixProvider
{
private const string AddInModifierTitle = "Add 'in' modifier";
private const string RemoveInModifierTitle = "Remove 'in' modifier";
public sealed override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(
InAnalyzerAnalyzer.CanBeInvokedWithInDiagnosticId,
InAnalyzerAnalyzer.DoNotUseInWithParameterDiagnosticId,
InAnalyzerAnalyzer.UseInWithParameterDiagnosticId);
public sealed override FixAllProvider GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
}
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var token = root.FindToken(context.Span.Start);
if (!token.Span.IntersectsWith(context.Span))
{
return;
}
var generator = SyntaxGenerator.GetGenerator(context.Document);
var node = generator.GetDeclaration(token.Parent);
if (node == null)
{
return;
}
foreach (var diagnostic in context.Diagnostics)
{
switch (diagnostic.Id)
{
case InAnalyzerAnalyzer.CanBeInvokedWithInDiagnosticId:
case InAnalyzerAnalyzer.UseInWithParameterDiagnosticId:
context.RegisterCodeFix(
CodeAction.Create(
AddInModifierTitle,
c => AddInModifierAsync(context.Document, node, c),
AddInModifierTitle),
diagnostic);
break;
case InAnalyzerAnalyzer.DoNotUseInWithParameterDiagnosticId:
context.RegisterCodeFix(
CodeAction.Create(
RemoveInModifierTitle,
c => RemoveInModifierAsync(context.Document, node, c),
RemoveInModifierTitle),
diagnostic);
break;
}
}
}
private async Task<Document> AddInModifierAsync(
Document document,
SyntaxNode node,
CancellationToken cancellationToken)
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
switch (node.Parent)
{
case ArgumentSyntax argumentSyntax:
editor.ReplaceNode(
argumentSyntax,
argumentSyntax.WithRefKindKeyword(SyntaxFactory.Token(SyntaxKind.InKeyword)));
break;
case ParameterSyntax parameterSyntax:
editor.ReplaceNode(
parameterSyntax,
parameterSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.InKeyword)));
break;
}
return editor.GetChangedDocument();
}
private async Task<Document> RemoveInModifierAsync(
Document document,
SyntaxNode node,
CancellationToken cancellationToken)
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
////switch (node.Parent)
////{
//// case ArgumentSyntax argumentSyntax:
//// editor.ReplaceNode(
//// argumentSyntax,
//// argumentSyntax.WithRefKindKeyword(SyntaxFactory.Token(SyntaxKind.InKeyword)));
//// break;
//// case ParameterSyntax parameterSyntax:
//// editor.ReplaceNode(
//// parameterSyntax,
//// parameterSyntax.AddModifiers(SyntaxFactory.Token(SyntaxKind.InKeyword)));
//// break;
////}
return editor.GetChangedDocument();
}
}
}
After finding this, which pointed to the CreateExpInstance tool, I tried resetting the "Roslyn"-suffixed experimental instance. That didn't solve my issue, but I then deleted my "Roslyn"-suffixed experimental instance and tried debugging my VSIX again. This time, debugging my VSIX worked and showed my CodeFixProvider.
rd /s/q "%LOCALAPPDATA%\Microsoft\VisualStudio\16.0_0f71fe5bRoslyn"
For anyone else suffering this stuff, try this:
Find the Reset visual studio experimental instance short cut command by using the Start search menu
Run it
Clean your solution
Run it in RELEASE mode
Notice that your stuff now appears in the instance
Close it
Switch back to DEBUG model
Et voila. Le black magique just happened. Problem Solv-ed.

Roslyn Vsix stopped working after moving the project

I had a working Roslyn VSIX Project for analyzing resources that could be localizable. Everything was working fine until I moved the project to a new location.
The analyzer's still seem to run and trigger the Code fix, However, the Code Action is never Registered into visual studio, so there is no option for that fix for some reason.
My analyzer:
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class ConstDiagnosticAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "Viventium.Localization.Tools.ConstantToResource";
// You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat.
// See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/Localizing%20Analyzers.md for more on localization
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.ConstAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.ConstAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.ConstAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
private const string Category = "Naming";
private static ResourceLocalizationRule localizationRule = new ResourceLocalizationRule();
private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeConstDeclaration, SyntaxKind.FieldDeclaration);
}
public static void AnalyzeConstDeclaration(SyntaxNodeAnalysisContext context)
{
var fieldDeclaration = (FieldDeclarationSyntax)context.Node;
if (false == IsValidConstDeclaration(context, fieldDeclaration))
{
return;
}
var firstVariable = fieldDeclaration.Declaration.Variables.FirstOrDefault();
var firstSymbol = context.SemanticModel.GetDeclaredSymbol(firstVariable);
context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation(), firstSymbol.Name));
}
private static bool VariableIsInResx(SyntaxNodeAnalysisContext context, FieldDeclarationSyntax fieldDeclaration)
{
var solution = context.GetSolution();
var documentPropertyMap = PropertyMapCache.GetDocumentPropertyMap(solution);
return localizationRule.IsFieldDeclaredInResx(fieldDeclaration, documentPropertyMap);
}
private static bool IsValidConstDeclaration(SyntaxNodeAnalysisContext context, FieldDeclarationSyntax fieldDeclaration)
{
if (false == fieldDeclaration.Modifiers.Any(SyntaxKind.ConstKeyword))
{
return false;
}
if (fieldDeclaration.Declaration.Variables.Count > 1)
{
return false;
}
var declaredVariable = fieldDeclaration.Declaration.Variables.FirstOrDefault();
var initializer = declaredVariable.Initializer;
if (initializer == null)
{
return false;
}
var constantValue = context.SemanticModel.GetConstantValue(initializer.Value);
if (!constantValue.HasValue)
{
return false;
}
var variableTypeName = fieldDeclaration.Declaration.Type;
var variableType = context.SemanticModel.GetTypeInfo(variableTypeName).ConvertedType;
if (variableType.SpecialType != SpecialType.System_String)
{
return false;
}
return true;
}
}
The Fix Provider:
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ConstantToResourceCodeFixProvider)), Shared]
public class ConstantToResourceCodeFixProvider : CodeFixProvider
{
#region Consts
private const string title = "Convert Constant Use Resources";
#endregion
#region CodeFixProvider Overrides
public sealed override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(ConstDiagnosticAnalyzer.DiagnosticId); }
}
public sealed override FixAllProvider GetFixAllProvider()
{
// See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
return WellKnownFixAllProviders.BatchFixer;
}
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
// TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf<FieldDeclarationSyntax>().First();
// Register a code action that will invoke the fix.
context.RegisterCodeFix(
CodeActionWithPreview.CreateStateful(
title: title,
createChangedDocument: (c, isPreview) => this.CheckConstForLocalization(context, declaration, isPreview, c),
equivalenceKey: title),
diagnostic);
}
#endregion
#region Analyzer Logic
private async Task<Document> CheckConstForLocalization(CodeFixContext context, FieldDeclarationSyntax field, bool isPreview, CancellationToken cancellationToken)
{
var documentPropertyMap = PropertyMapCache.GetDocumentPropertyMap(context.Document.Project.Solution);
var localizationRule = new ResourceLocalizationRule();
var updatedDocument = localizationRule.ConvertDocumentToUseResources(context.Document, documentPropertyMap, field, isPreview, cancellationToken);
return await updatedDocument;
}
#endregion
}
Can some one please give me a hint to of what is going on an how I can fix this?
After hours of debugging it turns out Diagnostic Analyzer's are not allowed to have periods in them...
public const string DiagnosticId = "Viventium_Localization_Tools_ConstantToResource";
The fix was quite simple I just changed the DiagnosticId to use underscores, and I've opened a bug with Roslyn

PRISM resolve interface with factory that uses type to resolve

I have been building a application with PRISM, this application is going to be groing alot over the coming years so it is set up modular and with IoC in mind.
In this application I am currently implementing Nlog for the logging mechanism.
Because I do not want to add a reference to NLog throughout my whole application I have created an interface for the logger
public interface ILog
{
void Log(LogLevel loglevel, string format);
void Log(LogLevel loglevel, string format, params object[] args);
void Log(LogLevel loglevel, Exception exc, string format, params object[] args);
}
I have also created an interface for the factory
public interface ILogFactory
{
ILog Initialize(Type type);
}
The factory takes a Type parameter that can be used to created a logger specific for that class. Now comes the challenge where I am going to use the ILog I would like to have a instance using the target types class.
public class ClassA
{
private ILog Logger { get; set;}
public ClassA(Ilog logger)
{
Logger = logger;
}
}
How can I wire my ILogFactory into PRISM so on a resolve of a class that uses Ilog that is uses the ILogFactory.Initialize(Type type) with in this case typeof(ClassA).
So after searching more of the web I finally found what I was looking for at
this blog
I had to modify the code a bid to make it not depend on log4net this resulted in the following code
A BuildTrackingExtension which we need to know where the logger we are going the create is needed.
public class BuildTrackingExtension : UnityContainerExtension
{
protected override void Initialize()
{
Context.Strategies.AddNew<BuildTrackingStrategy>(UnityBuildStage.TypeMapping);
}
public static IBuildTrackingPolicy GetPolicy(IBuilderContext context)
{
return context.Policies.Get<IBuildTrackingPolicy>(context.BuildKey, true);
}
public static IBuildTrackingPolicy SetPolicy(IBuilderContext context)
{
IBuildTrackingPolicy policy = new BuildTrackingPolicy();
context.Policies.SetDefault(policy);
return policy;
}
}
public class BuildTrackingStrategy : BuilderStrategy
{
public override void PreBuildUp(IBuilderContext context)
{
var policy = BuildTrackingExtension.GetPolicy(context)
?? BuildTrackingExtension.SetPolicy(context);
policy.BuildKeys.Push(context.BuildKey);
}
public override void PostBuildUp(IBuilderContext context)
{
IBuildTrackingPolicy policy = BuildTrackingExtension.GetPolicy(context);
if ((policy != null) && (policy.BuildKeys.Count > 0))
{
policy.BuildKeys.Pop();
}
}
}
public interface IBuildTrackingPolicy : IBuilderPolicy
{
Stack<object> BuildKeys { get; }
}
public class BuildTrackingPolicy : IBuildTrackingPolicy
{
public BuildTrackingPolicy()
{
BuildKeys = new Stack<object>();
}
public Stack<object> BuildKeys { get; private set; }
}
Then a LogCreationExtension to create the logger from my ILogFactory
public class LogCreationExtension : UnityContainerExtension
{
private ILogFactory LogFactory;
private LogCreationStrategy strategy;
public LogCreationExtension(ILogFactory logFactory)
{
LogFactory = logFactory;
}
protected override void Initialize()
{
strategy = new LogCreationStrategy(LogFactory);
Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
}
}
public class LogCreationStrategy : BuilderStrategy
{
public bool IsPolicySet { get; private set; }
private ILogFactory LogFactory;
public LogCreationStrategy(ILogFactory logFactory)
{
LogFactory = logFactory;
}
public override void PreBuildUp(IBuilderContext context)
{
Type typeToBuild = context.BuildKey.Type;
if (typeof(ILog).Equals(typeToBuild))
{
if (context.Policies.Get<IBuildPlanPolicy>(context.BuildKey) == null)
{
Type typeForLog = LogCreationStrategy.GetLogType(context);
IBuildPlanPolicy policy = new LogBuildPlanPolicy(typeForLog, LogFactory);
context.Policies.Set<IBuildPlanPolicy>(policy, context.BuildKey);
IsPolicySet = true;
}
}
}
public override void PostBuildUp(IBuilderContext context)
{
if (IsPolicySet)
{
context.Policies.Clear<IBuildPlanPolicy>(context.BuildKey);
IsPolicySet = false;
}
}
private static Type GetLogType(IBuilderContext context)
{
Type logType = null;
IBuildTrackingPolicy buildTrackingPolicy = BuildTrackingExtension.GetPolicy(context);
if ((buildTrackingPolicy != null) && (buildTrackingPolicy.BuildKeys.Count >= 2))
{
logType = ((NamedTypeBuildKey)buildTrackingPolicy.BuildKeys.ElementAt(1)).Type;
}
else
{
StackTrace stackTrace = new StackTrace();
//first two are in the log creation strategy, can skip over them
for (int i = 2; i < stackTrace.FrameCount; i++)
{
StackFrame frame = stackTrace.GetFrame(i);
logType = frame.GetMethod().DeclaringType;
if (!logType.FullName.StartsWith("Microsoft.Practices"))
{
break;
}
}
}
return logType;
}
}
public class LogBuildPlanPolicy : IBuildPlanPolicy
{
private ILogFactory LogFactory;
public LogBuildPlanPolicy(Type logType, ILogFactory logFactory)
{
LogType = logType;
LogFactory = logFactory;
}
public Type LogType { get; private set; }
public void BuildUp(IBuilderContext context)
{
if (context.Existing == null)
{
ILog log = LogFactory.Initialize(LogType);
context.Existing = log;
}
}
}
Wiring it up using unity
//Container.RegisterType<ILogFactory, NLogLogFactory>();
Container.RegisterInstance<ILogFactory>(_LogFactory);
Container.AddNewExtension<BuildTrackingExtension>();
Container.AddNewExtension<LogCreationExtension>();
I am using RegisterInstance because I am using the logfactory before InitializeShell is called

Categories

Resources