CodeAttributeArgument 類別

定義

表示用於中繼資料 (Metadata) 屬性宣告的引數。

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
繼承
CodeAttributeArgument
屬性

範例

下列程式代碼會建立類別,並新增程式代碼屬性來宣告類別可串行化且過時。

#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

備註

CodeAttributeArgument 可用來表示屬性建構函式之單一自變數的值,或用來初始化屬性屬性的值。

屬性 Value 會指出自變數的值。 使用時,屬性 Name 會指出要指派值之屬性的屬性名稱。

屬性宣告通常會以一些自變數初始化,這些自變數會在運行時間傳遞至屬性的建構函式。 若要為屬性的建構函式提供自變數,請將 每個自變數的 新增 CodeAttributeArgumentArgumentsCodeAttributeDeclaration集合。 Value只需要初始化每個 CodeAttributeArgument 的屬性。 集合內的自變數順序必須對應至屬性之建構函式方法簽章中的自變數順序。

您也可以提供 CodeAttributeArgument ,指出要設定的屬性名稱,以及要設定的值,藉此設定無法透過建構函式取得的屬性屬性。

建構函式

CodeAttributeArgument()

初始化 CodeAttributeArgument 類別的新執行個體。

CodeAttributeArgument(CodeExpression)

使用指定的值初始化 CodeAttributeArgument 類別的新執行個體。

CodeAttributeArgument(String, CodeExpression)

使用指定的名稱和數值,初始化 CodeAttributeArgument 類別的新執行個體。

屬性

Name

取得或設定屬性的名稱。

Value

取得或設定屬性引數的值。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱