This topic has not yet been rated - Rate this topic

CodeTypeDeclaration Class

Represents a type declaration for a class, structure, interface, or enumeration.

System.Object
  System.CodeDom.CodeObject
    System.CodeDom.CodeTypeMember
      System.CodeDom.CodeTypeDeclaration
        System.CodeDom.CodeTypeDelegate

Namespace:  System.CodeDom
Assembly:  System (in System.dll)
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[ComVisibleAttribute(true)]
public class CodeTypeDeclaration : CodeTypeMember

The CodeTypeDeclaration type exposes the following members.

  Name Description
Public method CodeTypeDeclaration() Initializes a new instance of the CodeTypeDeclaration class.
Public method CodeTypeDeclaration(String) Initializes a new instance of the CodeTypeDeclaration class with the specified name.
Top
  Name Description
Public property Attributes Gets or sets the attributes of the member. (Inherited from CodeTypeMember.)
Public property BaseTypes Gets the base types of the type.
Public property Comments Gets the collection of comments for the type member. (Inherited from CodeTypeMember.)
Public property CustomAttributes Gets or sets the custom attributes of the member. (Inherited from CodeTypeMember.)
Public property EndDirectives Gets the end directives for the member. (Inherited from CodeTypeMember.)
Public property IsClass Gets or sets a value indicating whether the type is a class or reference type.
Public property IsEnum Gets or sets a value indicating whether the type is an enumeration.
Public property IsInterface Gets or sets a value indicating whether the type is an interface.
Public property IsPartial Gets or sets a value indicating whether the type declaration is complete or partial.
Public property IsStruct Gets or sets a value indicating whether the type is a value type (struct).
Public property LinePragma Gets or sets the line on which the type member statement occurs. (Inherited from CodeTypeMember.)
Public property Members Gets the collection of class members for the represented type.
Public property Name Gets or sets the name of the member. (Inherited from CodeTypeMember.)
Public property StartDirectives Gets the start directives for the member. (Inherited from CodeTypeMember.)
Public property TypeAttributes Gets or sets the attributes of the type.
Public property TypeParameters Gets the type parameters for the type declaration.
Public property UserData Gets the user-definable data for the current object. (Inherited from CodeObject.)
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public event PopulateBaseTypes Occurs when the BaseTypes collection is accessed for the first time.
Public event PopulateMembers Occurs when the Members collection is accessed for the first time.
Top

CodeTypeDeclaration can be used to represent code that declares a class, structure, interface, or enumeration. CodeTypeDeclaration can be used to declare a type that is nested within another type.

The BaseTypes property specifies the base type or base types of the type being declared. The Members property contains the type members, which can include methods, fields, properties, comments and other types. The TypeAttributes property indicates the TypeAttributes values for the type declaration, which indicate the type category of the type. The IsClass, IsStruct, IsEnum, and IsInterface methods indicate whether the type is a class, structure, enumeration, or interface type, respectively.

Note Note

Some programming languages only support the declaration of reference types, or classes. To check a language-specific CodeDOM code generator for support for declaring interfaces, enumerations, or value types, call the Supports method to test for the appropriate GeneratorSupport flags. DeclareInterfaces indicates support for interfaces, DeclareEnums indicates support for enumerations, and DeclareValueTypes indicates support for value types such as structures.

You can build a class or a structure implementation in one complete declaration, or spread the implementation across multiple declarations. The IsPartial property indicates whether the type declaration is complete or partial. Not all code generators support partial type declarations, so you should test for this support by calling the Supports method with the flag PartialTypes.

This example demonstrates using a CodeTypeDeclaration to declare a type.


// Creates a new type declaration.
CodeTypeDeclaration newType = new CodeTypeDeclaration(
    // name parameter indicates the name of the type.
    "TestType");
// Sets the member attributes for the type to private.
newType.Attributes = MemberAttributes.Private;
// Sets a base class which the type inherits from.
newType.BaseTypes.Add( "BaseType" );            

// A C# code generator produces the following source code for the preceeding example code:

// class TestType : BaseType
// {
// }


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Sample code is misleading
The sample code provided for using this class is misleading with respect to establishing scope and visibility for a new type.  It illustrates the use of the Attributes property, when in fact the class scope and visibility are controlled via the TypeAttributes property.  The Attributes property is inherited from CodeTypeMember, and has no impact on the generated code for the class declaration.

The following C# code:
    CodeTypeDeclaration TypeDeclaration = new CodeTypeDeclaration("MyClass");
    TypeDeclaration.Attributes = MemberAttributes.Private;
yields this:
    public class MyClass {
    }

Whereas this code:
    CodeTypeDeclaration TypeDeclaration = new CodeTypeDeclaration("MyClass");
    TypeDeclaration.TypeAttributes = System.Reflection.TypeAttributes.NestedPrivate;
yields:
    private class MyClass {
    }