none
How do I create a nested class with CodeDom? RRS feed

  • Question

  • I can create an assembly ok with classes in a namespace 'spaceX'; e.g. 'spaceX.classY', but I need a nested class 'spaceX.classY.classZ'

    I create the CodeTypeDeclaration with the name 'classZ' (I tried 'classY.classZ' and it will not compile), then set the TypeAttributes to NestedPublic.

    I think I need to add this to the namespace 'spaceX.classY' - but when I search in CompileUnit.Namespaces for 'spaceX.classY' it is not found?

    If I add 'spaceX.classY' as a new namespace - there is an error on compiling 'namespace already defined'!?

    So how do I create a nested class using CodeDom (this was so easy using Reflection!)??

    - Tracey

    Friday, November 10, 2006 10:47 PM

Answers

  • You need to associate your nested class with its parent class.  A type declaration has the Members property which allows you to define the members of the type.  A CodeTypeDeclaration is a valid member so you need only add the nested type to the parent type.

    CodeTypeDeclaration typeParent = ...;
    CodeTypeDeclaration typeChild = ...;

    typeParent.Members.Add(typeChild);

    Michael Taylor - 11/11/06

    Saturday, November 11, 2006 5:53 PM
    Moderator

All replies

  • You need to associate your nested class with its parent class.  A type declaration has the Members property which allows you to define the members of the type.  A CodeTypeDeclaration is a valid member so you need only add the nested type to the parent type.

    CodeTypeDeclaration typeParent = ...;
    CodeTypeDeclaration typeChild = ...;

    typeParent.Members.Add(typeChild);

    Michael Taylor - 11/11/06

    Saturday, November 11, 2006 5:53 PM
    Moderator
  • Great - thanks a bunch!
    Monday, November 13, 2006 4:12 PM