CodeAttributeArgument Třída

Definice

Představuje argument použitý v deklaraci atributu metadat.

public ref class CodeAttributeArgument
public class CodeAttributeArgument
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeAttributeArgument
type CodeAttributeArgument = class
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type CodeAttributeArgument = class
Public Class CodeAttributeArgument
Dědičnost
CodeAttributeArgument
Atributy

Příklady

Následující kód vytvoří třídu a přidá atributy kódu, které deklarují, že třída je serializovatelná a zastaralá.

#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");

    // Use attributes to mark the class as serializable and obsolete.
    CodeAttributeDeclaration^ codeAttrDecl =
        gcnew CodeAttributeDeclaration("System.Serializable");
    class1->CustomAttributes->Add(codeAttrDecl);

    CodeAttributeArgument^ codeAttr =
        gcnew CodeAttributeArgument( gcnew CodePrimitiveExpression("This class is obsolete."));
    codeAttrDecl = gcnew CodeAttributeDeclaration("System.Obsolete", codeAttr);
    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.Serializable()]
//[System.Obsolete("This class is obsolete.")]
//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");

        // Use attributes to mark the class as serializable and obsolete.
        CodeAttributeDeclaration codeAttrDecl =
            new CodeAttributeDeclaration("System.Serializable");
        class1.CustomAttributes.Add(codeAttrDecl);

        CodeAttributeArgument codeAttr =
            new CodeAttributeArgument( new CodePrimitiveExpression("This class is obsolete."));
        codeAttrDecl = new CodeAttributeDeclaration("System.Obsolete", codeAttr);
        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.Serializable()]
// [System.Obsolete("This class is obsolete.")]
// public class Class1 {
// }
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")

        ' Use attributes to mark the class as serializable and obsolete.
        Dim codeAttrDecl As New CodeAttributeDeclaration("System.Serializable")
        class1.CustomAttributes.Add(codeAttrDecl)

        Dim codeAttr As _
            New CodeAttributeArgument( new CodePrimitiveExpression("This class is obsolete."))
        codeAttrDecl = New CodeAttributeDeclaration("System.Obsolete", codeAttr)
        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.Serializable(),  _
'  System.Obsolete("This class is obsolete.")>  _
' Public Class Class1
' End Class

Poznámky

CodeAttributeArgument lze použít buď k reprezentaci hodnoty pro jeden argument konstruktoru atributu, nebo hodnotu, pomocí které chcete inicializovat vlastnost atributu.

Vlastnost Value označuje hodnotu argumentu. Vlastnost Name při použití označuje název vlastnosti atributu, ke kterému se má přiřadit hodnota.

Deklarace atributů jsou často inicializovány pomocí řady argumentů, které jsou předány konstruktoru atributu za běhu. Chcete-li konstruktoru poskytnout argumenty pro atribut, přidejte CodeAttributeArgument pro každý argument do Arguments kolekce objektu CodeAttributeDeclaration. Inicializovat se Value musí pouze vlastnost každého z nich CodeAttributeArgument . Pořadí argumentů v rámci kolekce musí odpovídat pořadí argumentů v podpisu metody konstruktoru pro atribut.

Můžete také nastavit vlastnosti atributu, které nejsou k dispozici prostřednictvím konstruktoru zadáním parametru , CodeAttributeArgument který označuje název vlastnosti, která se má nastavit, spolu s hodnotou, která se má nastavit.

Konstruktory

CodeAttributeArgument()

Inicializuje novou instanci CodeAttributeArgument třídy .

CodeAttributeArgument(CodeExpression)

Inicializuje novou instanci CodeAttributeArgument třídy pomocí zadané hodnoty.

CodeAttributeArgument(String, CodeExpression)

Inicializuje novou instanci CodeAttributeArgument třídy pomocí zadaného názvu a hodnoty.

Vlastnosti

Name

Získá nebo nastaví název atributu.

Value

Získá nebo nastaví hodnotu pro argument atribut.

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Type Získá z aktuální instance.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro

Viz také