使用 Roslyn 检测 c# 6 功能 [英] Detect c# 6 features with Roslyn

查看:68
本文介绍了使用 Roslyn 检测 c# 6 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 roslyn 诊断分析器检测 c# 6 功能?

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

我有一个解决方案,该解决方案可以链接无法使用 c#6 功能的项目中的某些文件,因此我想仅针对这些文件将其设为错误.需要明确的是 - 我不能将整个项目设置为 c#5,只有一些文件是不受限制的.

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?

推荐答案

您可以使用此 Walker 来检测 C# 6 语法特性:

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);
    }
}

不幸的是,无法仅根据语法检查来检测是否在 6 版本上编写的文件,因为某些功能是内容相关的,例如 nameof 运算符(它可以是特殊的或常用方法)

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)

要测试 C# 6 功能,您可以使用 这个文件来自ANTLR语法库.

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

这篇关于使用 Roslyn 检测 c# 6 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆