Discover C # 6 features with Roslyn

Is there a way to detect C # 6 function using the roslyn diagnostic analyzer?

I have a solution that links in some files from a project that cannot use C # 6 functions, so I want to make this error only for these files. To be clear - I cannot install the whole project in C # 5, only some files do not work.

I can try and catch certain functions, but it is cumbersome and interesting, is there a faster way?

+5
source share
2 answers

You can use this Walker to discover the syntactic features of C # 6:

public class CSharp6FeaturesWalker : CSharpSyntaxWalker { public bool CSharp6Features { get; private set; } public CSharp6FeatureWalker() { } public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node) { if (node.ExpressionBody != null) { CSharp6Features = true; } else if (node.Initializer != null) { CSharp6Features = true; } base.VisitPropertyDeclaration(node); } public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { if (node.ExpressionBody != null) { CSharp6Features = true; } base.VisitMethodDeclaration(node); } public override void VisitOperatorDeclaration(OperatorDeclarationSyntax node) { if (node.ExpressionBody != null) { CSharp6Features = true; } base.VisitOperatorDeclaration(node); } public override void VisitConversionOperatorDeclaration(ConversionOperatorDeclarationSyntax node) { if (node.ExpressionBody != null) { CSharp6Features = true; } base.VisitConversionOperatorDeclaration(node); } public override void VisitIndexerDeclaration(IndexerDeclarationSyntax node) { if (node.ExpressionBody != null) { CSharp6Features = true; } base.VisitIndexerDeclaration(node); } public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node) { CSharp6Features = true; base.VisitConditionalAccessExpression(node); } public override void VisitInterpolatedStringExpression(InterpolatedStringExpressionSyntax node) { CSharp6Features = true; base.VisitInterpolatedStringExpression(node); } public override void VisitCatchFilterClause(CatchFilterClauseSyntax node) { CSharp6Features = true; base.VisitCatchFilterClause(node); } } 

Unfortunately, it is impossible to determine whether a file written in version 6 or not based solely on syntax checks, depending on some functions, depends on content, such as the nameof operator (it can be either in a special way or in the usual way)

For testing C # 6 functions, you can use this file from the ANTLR grammar repository.

+2
source

I believe the best way to do this is to use advanced build options. Go to the properties of your project and select the "Create" tab. In the lower right corner of this tab (you may have to scroll down) you should see the "Advanced" button. Click this and you will get this dialog:

Extra options

As you can see, you can change the language level for your specific project in C # 5.0. As soon as you do this and you try to use, say, string interpolation, you will be prompted with an error:

enter image description here

+1
source

All Articles