CodeAttributeArgument Klasa

Definicja

Reprezentuje argument używany w deklaracji atrybutu metadanych.

C#
public class CodeAttributeArgument
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeAttributeArgument
Dziedziczenie
CodeAttributeArgument
Atrybuty

Przykłady

Poniższy kod tworzy klasę i dodaje atrybuty kodu, aby zadeklarować, że klasa jest serializowalna i przestarzała.

C#
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 {
// }

Uwagi

CodeAttributeArgument Może służyć do reprezentowania wartości pojedynczego argumentu konstruktora atrybutu lub wartości, z którą należy zainicjować właściwość atrybutu.

Właściwość Value wskazuje wartość argumentu. Właściwość Name , gdy jest używana, wskazuje nazwę właściwości atrybutu, do której ma zostać przypisana wartość.

Deklaracje atrybutów są często inicjowane z wieloma argumentami przekazywanymi do konstruktora atrybutu w czasie wykonywania. Aby podać argumenty konstruktorowi atrybutu, dodaj CodeAttributeArgument argument dla każdego argumentu do Arguments kolekcji .CodeAttributeDeclaration Value Należy zainicjować tylko właściwość każdego z nichCodeAttributeArgument. Kolejność argumentów w kolekcji musi odpowiadać kolejności argumentów w podpisie metody konstruktora atrybutu.

Można również ustawić właściwości atrybutu, które nie są dostępne za pośrednictwem konstruktora, podając wartość wskazującą CodeAttributeArgument nazwę właściwości do ustawienia wraz z wartością do ustawienia.

Konstruktory

CodeAttributeArgument()

Inicjuje nowe wystąpienie klasy CodeAttributeArgument.

CodeAttributeArgument(CodeExpression)

Inicjuje CodeAttributeArgument nowe wystąpienie klasy przy użyciu określonej wartości.

CodeAttributeArgument(String, CodeExpression)

Inicjuje CodeAttributeArgument nowe wystąpienie klasy przy użyciu określonej nazwy i wartości.

Właściwości

Name

Pobiera lub ustawia nazwę atrybutu.

Value

Pobiera lub ustawia wartość argumentu atrybutu.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Produkt Wersje
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Zobacz też