CodeAttributeDeclaration CodeAttributeDeclaration CodeAttributeDeclaration CodeAttributeDeclaration Class

Definition

Represents an attribute declaration.

public ref class CodeAttributeDeclaration
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeAttributeDeclaration
type CodeAttributeDeclaration = class
Public Class CodeAttributeDeclaration
Inheritance
CodeAttributeDeclarationCodeAttributeDeclarationCodeAttributeDeclarationCodeAttributeDeclaration
Attributes

Examples

The following code example creates a CodeAttributeDeclaration that declares a CLSCompliantAttribute with an argument of false:

#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::CodeDom;
using namespace System::CodeDom::Compiler;

int main()
{
    // Declare a new type called Class1.
    CodeTypeDeclaration^ class1 = gcnew CodeTypeDeclaration("Class1");

    // Declare a new code attribute
    CodeAttributeDeclaration^ codeAttrDecl = gcnew CodeAttributeDeclaration(
        "System.CLSCompliantAttribute",
        gcnew CodeAttributeArgument(gcnew CodePrimitiveExpression(false)));
    class1->CustomAttributes->Add(codeAttrDecl);

    // Create a C# code provider
    CodeDomProvider^ provider = CodeDomProvider::CreateProvider("CSharp");

    // Generate code and send the output to the console
    provider->GenerateCodeFromType(class1, Console::Out, gcnew CodeGeneratorOptions());
}

// The CPP code generator produces the following source code for the preceeding example code:
//
//[System.CLSCompliantAttribute(false)]
//public class Class1 {
//}
using System;
using System.CodeDom;
using System.CodeDom.Compiler;

public class CodeGenExample
{
    static void Main()
    {
        // Declare a new type called Class1.
        CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");

        // Declare a new code attribute
        CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration(
            "System.CLSCompliantAttribute",
            new CodeAttributeArgument(new CodePrimitiveExpression(false)));
        class1.CustomAttributes.Add(codeAttrDecl);

        // Create a C# code provider
        CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

        // Generate code and send the output to the console
        provider.GenerateCodeFromType(class1, Console.Out, new CodeGeneratorOptions());
    }
}

// The C# code generator produces the following source code for the preceeding example code:
//
// [System.CLSCompliantAttribute(false)]
// public class Class1 {
// }
Imports System
Imports System.CodeDom
Imports System.CodeDom.Compiler

Public Class CodeGenExample

    Shared Sub Main
        ' Declare a new type called Class1.
        Dim class1 as New CodeTypeDeclaration("Class1")

        '  Declare a new code attribute
        Dim codeAttrDecl As New CodeAttributeDeclaration(
            "System.CLSCompliantAttribute",
            new CodeAttributeArgument(new CodePrimitiveExpression(false)))
        class1.CustomAttributes.Add(codeAttrDecl)

        ' Create a Visual Basic code provider
        Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")

        ' Generate code and send the output to the console
        provider.GenerateCodeFromType(class1, Console.Out, New CodeGeneratorOptions())
    End Sub

End Class

' The Visual Basic code generator produces the following source code for the preceeding example code:
'
' <System.CLSCompliantAttribute(false)>  _
' Public Class Class1
' End Class

Remarks

A CodeAttributeDeclaration can be used to represent an expression that declares an attribute. The attribute name and the arguments for the attribute are stored as properties of the object. A CodeAttributeArgument can be used to represent each argument for the attribute.

Constructors

CodeAttributeDeclaration() CodeAttributeDeclaration() CodeAttributeDeclaration() CodeAttributeDeclaration()

Initializes a new instance of the CodeAttributeDeclaration class.

CodeAttributeDeclaration(CodeTypeReference) CodeAttributeDeclaration(CodeTypeReference) CodeAttributeDeclaration(CodeTypeReference) CodeAttributeDeclaration(CodeTypeReference)

Initializes a new instance of the CodeAttributeDeclaration class using the specified code type reference.

CodeAttributeDeclaration(CodeTypeReference, CodeAttributeArgument[]) CodeAttributeDeclaration(CodeTypeReference, CodeAttributeArgument[]) CodeAttributeDeclaration(CodeTypeReference, CodeAttributeArgument[]) CodeAttributeDeclaration(CodeTypeReference, CodeAttributeArgument[])

Initializes a new instance of the CodeAttributeDeclaration class using the specified code type reference and arguments.

CodeAttributeDeclaration(String) CodeAttributeDeclaration(String) CodeAttributeDeclaration(String) CodeAttributeDeclaration(String)

Initializes a new instance of the CodeAttributeDeclaration class using the specified name.

CodeAttributeDeclaration(String, CodeAttributeArgument[]) CodeAttributeDeclaration(String, CodeAttributeArgument[]) CodeAttributeDeclaration(String, CodeAttributeArgument[]) CodeAttributeDeclaration(String, CodeAttributeArgument[])

Initializes a new instance of the CodeAttributeDeclaration class using the specified name and arguments.

Properties

Arguments Arguments Arguments Arguments

Gets the arguments for the attribute.

AttributeType AttributeType AttributeType AttributeType

Gets the code type reference for the code attribute declaration.

Name Name Name Name

Gets or sets the name of the attribute being declared.

Methods

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also