c# - 使用 Roslyn 查找所有不继承 C# 类并更改为从基础对象继承(类 Java)

标签 c# roslyn-code-analysis

我正在做一个 Roslyn 小项目,其中包括更改解析树和将更改写回文件。我已经开始使用独立代码分析器,并希望将其构建为命令行应用程序。不过,我遇到了挑战。 与: Find classes which derive from a specific base class with Roslyn部分地,主要是: https://github.com/dotnet/roslyn/wiki/Getting-Started-C%23-Syntax-Analysis我创建了这个小项目:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            if (args.Length < 1)
                throw new ArgumentException();
            SyntaxTree tree = CSharpSyntaxTree.ParseText(File.ReadAllText(args[0]));
            var root = (CompilationUnitSyntax) tree.GetRoot();
            var classes = from ClassDeclarationSyntax in root.DescendantNodesAndSelf() select ClassDeclarationSyntax;
            foreach (var c in classes)
            {
                if (/*Not inheriting*/)
                {
                    /*Add inherition*/
                }
            }
            /*Write changes to file*/
        }
        catch (Exception e)
        {
            Console.WriteLine("Fatal error ocured.");
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
        }
    }
}

正如代码中的注释所述,我需要检查该类是否继承自某些东西(并选择第二个选项)然后更改解析树并最后将其写入文件,现在我很高兴知道如何仅检查“非继承”,尽管也欢迎对第二步和第三步的任何指示。要解析和更改的文件由路径作为程序参数提供:

if (args.Length < 1)
    throw new ArgumentException();
SyntaxTree tree = CSharpSyntaxTree.ParseText(File.ReadAllText(args[0]));

**解决方案**
在收到的答案的支持下,我想出了可以工作的应用程序。 这是我的代码,可能不完美但可以工作
class Program
{
    static void Main(string[] args)
    {
        try
        {
            if (args.Length < 1)
                throw new ArgumentException();
            SyntaxTree tree = CSharpSyntaxTree.ParseText(File.ReadAllText(args[0]));
            var root = (CompilationUnitSyntax) tree.GetRoot();
            IdentifierNameSyntax iname = SyntaxFactory.IdentifierName("Object");
            BaseTypeSyntax bts = SyntaxFactory.SimpleBaseType(iname);
            SeparatedSyntaxList<BaseTypeSyntax> ssl = new SeparatedSyntaxList<BaseTypeSyntax>();
            ssl = ssl.Add(bts);
            BaseListSyntax bls = SyntaxFactory.BaseList(ssl);
            bool x = true;
            while(x) //Way to handle all nodes due to impossibility to handle more than one in foreach
            {
                foreach (var c in root.DescendantNodesAndSelf())
                {
                    x = false;
                    var classDeclaration = c as ClassDeclarationSyntax;
                    if (classDeclaration == null)
                        continue;
                    if (classDeclaration.BaseList != null) //Inherits
                        continue;
                    else //Not inherits
                    {
                        root = root.ReplaceNode(classDeclaration, classDeclaration.WithBaseList(bls));
                        x = true;
                        break;
                    }
                }
            }
            if (args.Length > 1) //Write to given file
                using (var sw = new StreamWriter(File.Open(args[1], FileMode.Open)))
                {
                    root.WriteTo(sw);
                }
            else //Overwrite source
                using (var sw = new StreamWriter(File.Open(args[0], FileMode.Open)))
                {
                    root.WriteTo(sw);
                }
        }
        catch (Exception e)
        {
            Console.WriteLine("Fatal error ocured.");
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
        }
    }
}

最佳答案

ClassDeclarationSyntax 具有包含 TypesBaseList。因此,您可以使用这些字段检索有关基类的信息:

        foreach (var c in correctRoot.DescendantNodesAndSelf())
        {
            var classDeclaration = c as ClassDeclarationSyntax;
            if (classDeclaration == null)
            {
                continue;
            }
            if (classDeclaration.BaseList?.Types.Count > 0)
            {
                Console.WriteLine("This class has base class or it implements interfaces");
            }
            else
            {
                /*Add inherition*/
            }
        }

不幸的是,您需要额外的逻辑来区分您的类具有基类还是仅实现接口(interface)。如果你想解决这个问题,你需要使用语义模型分析base object(类/接口(interface))以获取有关相应ISymbol 的信息 或尝试在 语法树 中查找这些节点的声明,如果此声明已在您的项目/解决方案中定义。

此外,如果您想向类添加继承,则需要使用 SyntaxFactory.SimpleBaseType(...)SyntaxFactory 将新创建的节点设置到 BaseList 中.BaseList(...)

关于c# - 使用 Roslyn 查找所有不继承 C# 类并更改为从基础对象继承(类 Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46286600/

相关文章:

c# - 控制流图,使用 Roslyn 进行内部/内部数据流分析,.NET 编译器平台

c# - 获取基本 IQueryable 实例的 Count()

c# - 如何在访问文件夹时收到通知?

c# - 如何确定 FieldDeclarationSyntax 的类型是否是 Roslyn 中的接口(interface)?

c# - 启用 Microsoft 对 .NET Core 项目的代码分析

c# - Roslyn - 查找对具有调用详细信息的方法的所有引用(具体的通用参数)

c# - 类型加载异常

c# - Serilog 没有写入 MsSql 服务器数据库,也没有显示任何错误

c# - 32 位操作系统上的 IntPtr 和 64 位操作系统上的 UInt64

c# - SyntaxWalker 访问者不访问方法