CodeAttributeArgument 类

定义

表示在元数据特性声明中使用的参数。Represents an argument used in a metadata attribute declaration.

public ref class CodeAttributeArgument
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeAttributeArgument
type CodeAttributeArgument = class
Public Class CodeAttributeArgument
继承
CodeAttributeArgument
属性

示例

下面的代码创建一个类, 并添加代码属性以声明该类可序列化且已过时。The following code creates a class, and adds code attributes to declare that the class is serializable and obsolete.

#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可用于表示属性构造函数的单个参数的值, 或用于初始化属性的属性的值。CodeAttributeArgument can be used to represent either the value for a single argument of an attribute constructor, or a value with which to initialize a property of the attribute.

Value属性指示参数的值。The Value property indicates the value of the argument. 使用Name属性时, 它指示要将值分配到的属性的属性名称。The Name property, when used, indicates the name of a property of the attribute to which to assign the value.

特性声明频繁地使用多个在运行时传递给特性的构造函数的参数进行初始化。Attribute declarations are frequently initialized with a number of arguments that are passed into the constructor of the attribute at run time. 若要为特性的构造函数提供参数, 请将CodeAttributeArgument每个自变量的Arguments添加到的CodeAttributeDeclaration集合中。To provide arguments to the constructor for an attribute, add a CodeAttributeArgument for each argument to the Arguments collection of a CodeAttributeDeclaration. 只需要初始化每个CodeAttributeArgument的属性。ValueOnly the Value property of each CodeAttributeArgument needs to be initialized. 集合内参数的顺序必须与属性的构造函数的方法签名中的参数顺序对应。The order of arguments within the collection must correspond to the order of arguments in the method signature of the constructor for the attribute.

你还可以通过提供CodeAttributeArgument指示要设置的属性的名称的, 以及要设置的值, 来设置无法通过构造函数使用的属性的属性。You can also set properties of the attribute that are not available through the constructor by providing a CodeAttributeArgument that indicates the name of the property to set, along with the value to set.

构造函数

CodeAttributeArgument()

初始化 CodeAttributeArgument 类的新实例。Initializes a new instance of the CodeAttributeArgument class.

CodeAttributeArgument(CodeExpression)

使用指定的值初始化 CodeAttributeArgument 类的新实例。Initializes a new instance of the CodeAttributeArgument class using the specified value.

CodeAttributeArgument(String, CodeExpression)

使用指定的名称和值初始化 CodeAttributeArgument 类的新实例。Initializes a new instance of the CodeAttributeArgument class using the specified name and value.

属性

Name

获取或设置特性的名称。Gets or sets the name of the attribute.

Value

获取或设置特性参数的值。Gets or sets the value for the attribute argument.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅