Detect c# 6 features with Roslyn

非 Y 不嫁゛ 提交于 2019-12-07 06:07:09

问题


Is there a way to detect a c# 6 feature with a roslyn diagnostic analyzer?

I have a solution that links in some files from a project that cannot use c#6 features, so I want to make that an error just for those files. To be clear - I cannot set the whole project to c#5, only some files are off limits.

I can try and catch specific features, but it's cumbersome and was wondering if there is a faster way?


回答1:


You can use this Walker for detecting C# 6 syntax features:

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 not possible to detect whether the file written on 6 version or not based only on syntax checks because of some features are content-depended such as nameof operator (it can be both special or usual method)

For testing C# 6 features you can use this file from ANTLR grammars repository.




回答2:


I believe the best way to go about this is to use the advanced build options. Go to your project properties and select the "Build" tab. On the bottom-right of that tab (you may have to scroll down) you should see an "Advanced" button. Click that and you'll get this dialog:

As you see, you can change the language level for your particular project to be C# 5.0. Once you do that, and you try to use, say, string interpolation, you'll be prompted with an error:



来源:https://stackoverflow.com/questions/37341634/detect-c-sharp-6-features-with-roslyn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!