CodeAttributeArgument クラス

定義

メタデータ属性宣言で使用する引数を表します。

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 を使用して、属性コンストラクターの 1 つの引数の値、または 属性のプロパティを初期化する値を表すことができます。

プロパティは Value 、引数の値を示します。 プロパティを Name 使用する場合は、値を割り当てる属性のプロパティの名前を示します。

属性宣言は、多くの場合、実行時に属性のコンストラクターに渡される引数の数で初期化されます。 属性のコンストラクターに引数を指定するには、 のコレクションに各引数の CodeAttributeDeclarationArguments追加CodeAttributeArgumentします。 各 CodeAttributeArgumentValue プロパティのみを初期化する必要があります。 コレクション内の引数の順序は、 属性のコンストラクターのメソッド シグネチャ内の引数の順序に対応している必要があります。

また、設定するプロパティの名前を示す を、設定する値と共に指定 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)

適用対象

こちらもご覧ください