0

Currently I'm working on a project with NRefactory. We're filtering typedeclarations like 'Class' and 'Interface' out of a .cs file. 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"));
Mittchel
  • 1,896
  • 3
  • 19
  • 37
  • What do you mean "is not working"? What *does* it do? I *think* the code you have would turn `class C {}` into `namespace customNamespace {} class C{}`, which is not what you want. – svick Oct 14 '13 at 14:18
  • True that's not what I want.. Well, when I use: ToString() the whole namespace attribute is missing, so it's not inserted – Mittchel Oct 14 '13 at 14:24

1 Answers1

1

I've never tried to insert a Namespace node before the TypeDeclaration. Instead I've added the Namespace first and then added the 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);
        }
    }
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123