简体   繁体   English

NRefactory在名称声明中添加名称空间

[英]NRefactory add namespace to typedeclaration

Currently I'm working on a project with NRefactory. 目前,我正在与NRefactory合作进行项目。 We're filtering typedeclarations like 'Class' and 'Interface' out of a .cs file. 我们正在从.cs文件中过滤掉类型声明,例如“类”和“接口”。 We would like to place these typedeclarations into a custom namespace, but for some reason it's not working. 我们希望将这些类型声明放入自定义名称空间中,但是由于某种原因,它无法正常工作。 Is anyone able to assist me with this problem? 有人可以协助我解决这个问题吗?

I've tried the following code: 我尝试了以下代码:

typeDeclaration.Parent.InsertChildBefore(typeDeclaration, ns, new Role<NamespaceDeclaration>("customNamespace"));

I've never tried to insert a Namespace node before the TypeDeclaration. 我从未尝试在TypeDeclaration 之前插入名称空间节点。 Instead I've added the Namespace first and then added the TypeDeclaration: 相反,我先添加了命名空间,然后添加了TypeDeclaration:

  public static void AddChildTypeDeclaration(
        this AstNode tree, 
        TypeDeclaration newClass,
        NamespaceDeclaration parentNamespace = null)
    {
        if (null != parentNamespace)
        {
            var newNamespaceNode = new NamespaceDeclaration(
                parentNamespace.Name);

            newNamespaceNode.AddMember(newClass);

            tree.AddChild(newNamespaceNode, SyntaxTree.MemberRole);
        }
        else
        {
            tree.AddChild(newClass, Roles.TypeMemberRole);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM