This documentation is archived and is not being maintained.

CodeAttributeArgument Class

Represents an argument used in a metadata attribute declaration.

Namespace:  System.CodeDom
Assembly:  System (in System.dll)

public 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.

The Value property indicates the value of the argument. 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. To provide arguments to the constructor for an attribute, add a CodeAttributeArgument for each argument to the Arguments collection of a CodeAttributeDeclaration. Only 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.

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.

The following code creates a class, and adds code attributes to declare that the class is serializable and obsolete.

    // 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(

    CodeAttributeArgument codeAttr = new CodeAttributeArgument(
               new CodePrimitiveExpression("This class is obsolete."));
    codeAttrDecl = new CodeAttributeDeclaration("System.Obsolete", 
// A C# code generator produces the following source code for the preceeding example code:
//    [System.Serializable()]
//    [System.Obsolete("This class is obsolete.")]
//    public class Class1 {
//    }


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0