CodeTypeDeclaration Class
The System.CodeDom.CodeTypeDeclaration
class represents a type declaration for a class, structure, interface, or enumeration.
Namespace
System.CodeDom
Assembly
System.dll
Syntax
public class CodeTypeDeclaration : CodeObject
{
// Constructors
public CodeTypeDeclaration();
public CodeTypeDeclaration(string name);
public CodeTypeDeclaration(string name, TypeAttributes attributes);
// Properties
public CodeAttributeDeclarationCollection CustomAttributes { get; }
public CodeTypeReference BaseTypes { get; }
public CodeTypeMemberCollection Members { get; }
public string Name { get; set; }
public TypeAttributes TypeAttributes { get; set; }
// ... other members
}
Members
Constructors
+
CodeTypeDeclaration()
Initializes a new instance of the CodeTypeDeclaration
class.
var type = new CodeTypeDeclaration();
CodeTypeDeclaration(string name)
Initializes a new instance of the CodeTypeDeclaration
class with the specified name.
var type = new CodeTypeDeclaration("MyClass");
CodeTypeDeclaration(string name, TypeAttributes attributes)
Initializes a new instance of the CodeTypeDeclaration
class with the specified name and attributes.
var type = new CodeTypeDeclaration("MyClass", TypeAttributes.Public);
Properties
+
Property | Type | Description |
---|---|---|
Name | string | Gets or sets the name of the type. |
TypeAttributes | TypeAttributes | Gets or sets the type attributes (e.g., public, abstract). |
BaseTypes | CodeTypeReferenceCollection | A collection that specifies the base type(s) for this type. |
Members | CodeTypeMemberCollection | A collection of members (methods, fields, properties, etc.) declared in the type. |
CustomAttributes | CodeAttributeDeclarationCollection | Custom attributes applied to the type. |
Methods
+
InitDefaults()
Initializes the default values for the type declaration.
type.InitDefaults();
Example
This example demonstrates how to generate a simple class with a field and a method using CodeDom
.
Show Code
+
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
class Program
{
static void Main()
{
// Create a compile unit
CodeCompileUnit compileUnit = new CodeCompileUnit();
// Declare a namespace
CodeNamespace ns = new CodeNamespace("GeneratedNamespace");
ns.Imports.Add(new CodeNamespaceImport("System"));
compileUnit.Namespaces.Add(ns);
// Declare a class
CodeTypeDeclaration myClass = new CodeTypeDeclaration("MyClass")
{
IsClass = true,
TypeAttributes = System.Reflection.TypeAttributes.Public
};
ns.Types.Add(myClass);
// Add a field
CodeMemberField field = new CodeMemberField(typeof(int), "_value");
field.Attributes = MemberAttributes.Private;
myClass.Members.Add(field);
// Add a property
CodeMemberProperty prop = new CodeMemberProperty
{
Name = "Value",
Type = new CodeTypeReference(typeof(int)),
Attributes = MemberAttributes.Public | MemberAttributes.Final,
GetStatements = { new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_value")) },
SetStatements = { new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_value"), new CodePropertySetValueReferenceExpression()) }
};
myClass.Members.Add(prop);
// Add a method
CodeMemberMethod method = new CodeMemberMethod
{
Name = "Display",
ReturnType = new CodeTypeReference(typeof(void)),
Attributes = MemberAttributes.Public
};
method.Statements.Add(new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("Console"),
"WriteLine",
new CodePrimitiveExpression("Value is: {0}"),
new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_value")
));
myClass.Members.Add(method);
// Generate C# code
CSharpCodeProvider provider = new CSharpCodeProvider();
using (var sourceWriter = new System.IO.StringWriter())
{
provider.GenerateCodeFromCompileUnit(compileUnit, sourceWriter, new CodeGeneratorOptions());
Console.WriteLine(sourceWriter.ToString());
}
}
}