CodeAttributeDeclaration 类

定义

表示特性声明。Represents an attribute declaration.

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

示例

下面的代码示例创建一个CodeAttributeDeclaration , 它CLSCompliantAttribute使用参数false声明:The following code example creates a CodeAttributeDeclaration that declares a CLSCompliantAttribute with an argument of false:

#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");

    // Declare a new code attribute
    CodeAttributeDeclaration^ codeAttrDecl = gcnew CodeAttributeDeclaration(
        "System.CLSCompliantAttribute",
        gcnew CodeAttributeArgument(gcnew CodePrimitiveExpression(false)));
    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.CLSCompliantAttribute(false)]
//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");

        // Declare a new code attribute
        CodeAttributeDeclaration codeAttrDecl = new CodeAttributeDeclaration(
            "System.CLSCompliantAttribute",
            new CodeAttributeArgument(new CodePrimitiveExpression(false)));
        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.CLSCompliantAttribute(false)]
// 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")

        '  Declare a new code attribute
        Dim codeAttrDecl As New CodeAttributeDeclaration(
            "System.CLSCompliantAttribute",
            new CodeAttributeArgument(new CodePrimitiveExpression(false)))
        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.CLSCompliantAttribute(false)>  _
' Public Class Class1
' End Class

注解

CodeAttributeDeclaration可用于表示声明属性的表达式。A CodeAttributeDeclaration can be used to represent an expression that declares an attribute. 特性名称和特性的参数作为对象的属性存储。The attribute name and the arguments for the attribute are stored as properties of the object. CodeAttributeArgument可用于表示属性的每个参数。A CodeAttributeArgument can be used to represent each argument for the attribute.

构造函数

CodeAttributeDeclaration()

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

CodeAttributeDeclaration(CodeTypeReference)

使用指定的代码类型引用初始化 CodeAttributeDeclaration 类的新实例。Initializes a new instance of the CodeAttributeDeclaration class using the specified code type reference.

CodeAttributeDeclaration(CodeTypeReference, CodeAttributeArgument[])

使用指定的代码类型引用和参数初始化 CodeAttributeDeclaration 类的新实例。Initializes a new instance of the CodeAttributeDeclaration class using the specified code type reference and arguments.

CodeAttributeDeclaration(String)

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

CodeAttributeDeclaration(String, CodeAttributeArgument[])

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

属性

Arguments

获取特性的参数。Gets the arguments for the attribute.

AttributeType

获取代码特性声明的代码类型引用。Gets the code type reference for the code attribute declaration.

Name

获取或设置要声明的特性的名称。Gets or sets the name of the attribute being declared.

方法

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)

适用于

另请参阅