Sdílet prostřednictvím


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 k deklaraci, že třída je serializovatelná a zastaralá.

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 k reprezentaci buď hodnoty pro jeden argument konstruktoru atributu, nebo hodnotu, se kterou chcete inicializovat vlastnost atributu.

Vlastnost Value označuje hodnotu argumentu. Při Name použití určuje název vlastnosti atributu, ke kterému má být hodnota přiřazena.

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

Vlastnosti atributu, které nejsou dostupné prostřednictvím konstruktoru, můžete také nastavit zadáním CodeAttributeArgument názvu vlastnosti, která se má nastavit, spolu s hodnotou, která se má nastavit.

Konstruktory

Name Description
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 Description
Name

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

Value

Získá nebo nastaví hodnotu argumentu atributu.

Metody

Name Description
Equals(Object)

Určuje, zda je zadaný objekt roven aktuálnímu objektu.

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

Slouží jako výchozí funkce hash.

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

Získá Type aktuální instance.

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

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

(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é