I'm creating a CodeFixProvider for the analyzer that is detecting if MessagePackObject attribute is missing from the class declaration. Beside, My attribute need to have one argument keyAsPropertyName with value true
[MessagePackObject(keyAsPropertyName:true)]
I have done adding attribute without arguments like so(my solution method)
private async Task<Solution> AddAttributeAsync(Document document, ClassDeclarationSyntax classDecl, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
var attributes = classDecl.AttributeLists.Add(
SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("MessagePackObject"))
// .WithArgumentList(SyntaxFactory.AttributeArgumentList(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.AttributeArgument(SyntaxFactory.("keyAsPropertyName")))))))
// .WithArgumentList(...)
)).NormalizeWhitespace());
return document.WithSyntaxRoot(
root.ReplaceNode(
classDecl,
classDecl.WithAttributeLists(attributes)
)).Project.Solution;
}
But I don't know how add attribute with argument having value. Can somebody help me please?
[MessagePackObject(keyAsPropertyName:true)] is a AttributeArgumentSyntax that has NameColons and doesn't have NameEquals, so you just need to create it passing nothing as NameEquals and passing the correct initial expression like this:
...
var attributeArgument = SyntaxFactory.AttributeArgument(
null, SyntaxFactory.NameColon("keyAsPropertyName"), SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression));
var attributes = classDecl.AttributeLists.Add(
SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("MessagePackObject"))
.WithArgumentList(SyntaxFactory.AttributeArgumentList(SyntaxFactory.SingletonSeparatedList(attributeArgument)))
)).NormalizeWhitespace());
...
Related
I am checking for the possibility to build custom roslyn analyzer for case specifics to our system.
The solution is in .net Framework 4.8. I started with the tutorial How to write csharp analyzer code fix and am making my way from there.
The first case I want to check is that when the programmer use the value of a specific service they must not assume that the result is not null.
Take this service definition :
public interface IConfigurationService
{
Task<IConfiguration> GetConfiguration(string clientId);
}
And a code sample to analyze :
public async Task DoSomeTask(string clientId)
{
var configuration = await _configurationService.GetConfiguration(clientId);
// This line should raise a warning because this specific client may not be configurated
var serviceUri = configuration.ServiceUri;
DoSomeSubTask(serviceUri);
}
So far I got this
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
// The goal is to target the variable declaration (var configuration = ...)
context.RegisterSyntaxNodeAction(
AnalyzeDecalaration,
SyntaxKind.LocalDeclarationStatement
);
}
private static void AnalyzeDecalaration(SyntaxNodeAnalysisContext context)
{
// Check for the type of the variable and exit if it is not 'IConfiguration'
var symbolInfo = context.SemanticModel.GetSymbolInfo(localDeclaration.Declaration.Type);
var typeSymbol = symbolInfo.Symbol;
if (typeSymbol.Name != "IConfiguration")
{
return;
}
// Stuck here. I'm pretty sure dataFlowAnalysis is the key, but I can't figure how to use it
var dataFlowAnalysis = context.SemanticModel.AnalyzeDataFlow(localDeclaration);
var variable = localDeclaration.Declaration.Variables.Single();
ISymbol variableSymbol = context.SemanticModel.GetDeclaredSymbol(
variable,
context.CancellationToken
);
}
So that's where I am. I have targeted variable declaration for the target type. Not very much.
Since it is a specific case for a specific type, the analysis does not have to be very fancy. For exemple, I don't need to check for instanaces of IConfiguration inside an array, that's not a thing in our code base. Basically juste property access without null check.
In my application, different canvases are stored as "pages", the contents of each canvas is stored as "cells".
Now when I want to load all cells that occupy / make up one canvas, I retrieve them like this:
public Task<List<Cell>> GetCellsAsync(string uPageGUID)
{
return database.QueryAsync<Cell>("SELECT * FROM cells WHERE cellpageguid = ?", uPageGUID);
}
This works great.
Now I would like to find out the "pageguid" of the page that has the value "pageisstartpage" set to true.
Therefore I'm trying the following:
public Task<string>GetStartPageGUID()
{
nPages<List<Page>>=database.QueryAsync<Page>("SELECT * FROM pages WHERE pageisstartpage=?", true);
return nPages.First.GUID;
}
The compiler tells me:
nPages doesn't exist in the current context.
I don't see where I made a mistake.
nPages doesn't exist in the current context....I don't see where I made a mistake.
The first thing to mention is that the declaration of the List<Page> seems backwards.
nPages<List<Page>>=database....
The type has to be written first followed by the variable name.
List<Page> nPagesTask = database...
Another interpretation could be that you have a generic type variable nPages in which you want to specify the generic type. So the compiler looks whether this variable has already been declared. And apparently it cannot find any.
The second thing If you have an async method that returns a Task<string> you could do the following:
public async Task<string>GetStartPageGUID()
{
Task<List<Page>> nPagesTask = database.QueryAsync<Page>("SELECT * FROM pages WHERE pageisstartpage=?", true);
List<Page> npages = await nPagesTask;
return nPages.First().GUID;
}
Here is the source of the QueryAsync method. this is the signature:
public Task<List<T>> QueryAsync<T> (string query, params object[] args)
so it returns a Task<List<T>>. Since your method specifies a different return type the usual pattern is to await it in a async method as described in the MSDN example and then return the type that you specified in you method.
You have to declare nPages correctly:
List<Page> nPages = database.QueryAsync<Page>("SELECT * FROM pages WHERE pageisstartpage=?", true);
Since a lot of attributes are designed by avoid unsealed attributes I'm searching for a solution for setting an attributes value (my first idea was to inherit the class and set a constructor what checks the web-config - not possible with a sealed class):
There's the ApiExplorerSettingsAttribute in namespace System.Web.Http.Description
I want the following API-action to get hidden in the case, a value in web-config is false:
<Api.Properties.Settings>
<setting name="Hoster">
<value>False</value>
</setting>
</Api.Properties.Settings>
the action would look like this:
[HttpGet, Route("api/bdlg")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(BdlgDataStorage))]
[ApiExplorerSettings(IgnoreApi = Properties.Settings.Default.Hoster)]
private async Task<BdlgDataStorage> GetBdlgStorageValues()
{
using (var context = new BdlgContext())
return context.BdlgDataStorages
.Include(s=>s.ChangeTrack)
.Where(w=>w.Isle > 56)
.Select(selectorFunction)
.ToListAsync();
}
The important line is:
[ApiExplorerSettings(IgnoreApi = Properties.Settings.Default.Hoster)]
Here, I get a compiler-error:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
Anyone got any idea, how I can set the value of IgnoreApi the same as the one from web-config?
You can't. Attributes are compiled statically into the assembly. They belong to the metadata of a member. You can't change attributes at run-time.
You have to find another way to influence the ApiExplorerSettings. This post seems to be what you are looking for: Dynamically Ignore WebAPI method on controller for api explorer documentation.
Another possible solution I found is by using preprocessor directives (this would have been enough for me, since the action only should be visible in swagger when it's DEBUG):
#if DEBUG
[ApiExplorerSettings(IgnoreApi = false)]
#else
[ApiExplorerSettings(IgnoreApi = true)]
#endif
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(BdlgDataStorage))]
[HttpGet, Route("api/bdlg")]
private async Task<BdlgDataStorage> GetBdlgStorageValues()
{
using (var context = new BdlgContext())
return context.BdlgDataStorages
.Include(s=>s.ChangeTrack)
.Where(w=>w.Isle > 56)
.Select(selectorFunction)
.ToListAsync();
}
I'm building CodeFixProvider for the analyzer that is detecting if custom attribute is missing from the method declaration. Basically custom attribute that should be added to the method looks like
[CustomAttribute(param1: false, param2: new int[]{1,2,3})]
this is what I've got so far:
public sealed override async Task RegisterCodeFixesAsync( CodeFixContext context ) {
var root = await context.Document.GetSyntaxRootAsync( context.CancellationToken ).ConfigureAwait( false );
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var declaration = root.FindToken( diagnosticSpan.Start ).Parent.AncestorsAndSelf( ).OfType<MethodDeclarationSyntax>( ).First( );
context.RegisterCodeFix(
CodeAction.Create(
title: title,
createChangedSolution: c => this.AddCustomAttribute(context.Document, declaration, c),
equivalenceKey: title),
diagnostic);
}
private async Task<Solution> AddCustomAttribute( Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken ) {
// I suspect I need to do something like methodDeclaration.AddAttributeLists(new AttributeListSyntax[] {
// but not sure how to use it exactly
throw new NotImplementedException( );
}
Remember, roslyn syntax trees are immutable. You'll need something like:
private async Task<Solution> AddCustomAttribute(Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
var attributes = methodDeclaration.AttributeLists.Add(
SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList<AttributeSyntax>(
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("CustomAttribute"))
// .WithArgumentList(...)
)).NormalizeWhitespace());
return document.WithSyntaxRoot(
root.ReplaceNode(
methodDeclaration,
methodDeclaration.WithAttributeLists(attributes)
)).Project.Solution;
}
To get the full SyntaxFactory code for the attribute constructor .WithArgumentList() throw it into the Roslyn Quoter.
I am trying to create a new property that has the same body as a method.
Here is my code so far:
private async Task<Solution> ConvertMethodToProperty(Document document, MethodDeclarationSyntax methodNode, CancellationToken cancellationToken)
{
AccessorListSyntax accesors = SyntaxFactory.AccessorList(new SyntaxList<AccessorDeclarationSyntax>
{
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, methodNode.Body)
});
PropertyDeclarationSyntax newp = SyntaxFactory.PropertyDeclaration(new SyntaxList<AttributeListSyntax>(), methodNode.Modifiers, methodNode.ReturnType, methodNode.ExplicitInterfaceSpecifier, methodNode.Identifier, accesors);
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
document = document.WithSyntaxRoot(root.ReplaceNode(methodNode, newp));
return document.Project.Solution;
}
}
However when I run this on my test project I see this:
Even though the methodNode.Body from the code is populated with the method body I want:
What am I doing wrong when I create my AccessorListSyntax? Thanks!
I think your use of the collection initialiser on the SyntaxList is not valid. As that will implicitly call the Add method on your SyntaxList but the add doesn't modify the underlying collection it just returns a new SyntaxList. Try instantiating and manually adding afterwards like this.
var accessorList = new SyntaxList<AccessorDeclarationSyntax>();
accessorList = accessorList.Add(SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, methodNode.Body));
I fell into the same trap when I started playing around with Roslyn, just have to remember the immutability thing.