Compartilhar via


CodeAttribute2 Interface

Defines an attribute for a code element.

Namespace:  EnvDTE80
Assembly:  EnvDTE80 (in EnvDTE80.dll)

Syntax

'Declaration
<GuidAttribute("35187E2A-E5F6-4F89-A4CE-DA254640855B")> _
Public Interface CodeAttribute2 _
    Inherits CodeAttribute
[GuidAttribute("35187E2A-E5F6-4F89-A4CE-DA254640855B")]
public interface CodeAttribute2 : CodeAttribute
[GuidAttribute(L"35187E2A-E5F6-4F89-A4CE-DA254640855B")]
public interface class CodeAttribute2 : CodeAttribute
[<GuidAttribute("35187E2A-E5F6-4F89-A4CE-DA254640855B")>]
type CodeAttribute2 =  
    interface 
        interface CodeAttribute 
    end
public interface CodeAttribute2 extends CodeAttribute

The CodeAttribute2 type exposes the following members.

Properties

  Name Description
Public property Arguments Gets a collection of CodeElement objects that contains the CodeAttributeArgument objects associated with this attribute.
Public property Children Gets a collection of objects contained within this code construct.
Public property Collection Gets a collection of CodeAttribute2 objects.
Public property DTE Gets the top-level extensibility object.
Public property EndPoint Gets the edit point that is the end location of the code attribute.
Public property Extender Returns the requested Extender if it is available for this code attribute.
Public property ExtenderCATID Gets the Extender category ID (CATID) for the object.
Public property ExtenderNames Gets a list of names of available Extenders for the object.
Public property FullName Gets the full path and name of the object's file.
Public property InfoLocation Gets the code model.
Public property IsCodeType Gets whether a CodeType object can be obtained from this object.
Public property Kind Gets an enumeration indicating the type of attribute.
Public property Language Gets a constant identifying the programming language used to author the attribute.
Public property Name Sets or gets the name of the code attribute.
Public property Parent Gets the immediate parent object of the code attribute.
Public property ProjectItem Gets the ProjectItem associated with the code attribute.
Public property StartPoint Gets a TextPoint that defines the beginning of the attribute.
Public property Target Sets or gets the target of the code attribute.
Public property Value Sets or gets the data for the code attribute.

Top

Methods

  Name Description
Public method AddArgument Adds an argument to the attribute.
Public method Delete Removes all attributes in the code element.
Public method GetEndPoint Returns a TextPoint object that marks the end position of the attribute.
Public method GetStartPoint Returns a TextPoint object that defines the beginning position of the attribute.

Top

Remarks

The CodeAttribute2 object represents a single COM metadata attribute associated with a code element. You can add new attributes with the AddAttribute method, and delete the attributes by using the Delete method on the appropriate object. You can get and set the value of a code attribute by using this object.

Note

The values of code model elements such as classes, structs, functions, attributes, delegates, and so forth can be non-deterministic after making certain kinds of edits, meaning that their values cannot be relied upon to always remain the same. For more information, see the section Code Model Element Values Can Change in Discovering Code by Using the Code Model (Visual Basic).

Examples

// The following example creates a new namespace and attribute in the current class.
public void CreateClassAndAttrib(DTE2 applicationObject)
{
    // Before running, load or create a project.
    FileCodeModel2 fcm2 = GetFileCodeModel(applicationObject);
    CodeAttribute2 cmAttribute;
    CodeClass2 cmClass;

    if (fcm2 != null)
    {
        CodeNamespace cmNamespace;
        // Try to create a new namespace.
        try
        {
            cmNamespace = fcm2.AddNamespace("CMNamespace", -1);
            // If successful, create the other code elements.
            if (cmNamespace != null)
            {
                cmClass = (CodeClass2)cmNamespace.AddClass("ANewClass", 
                -1, null, null, vsCMAccess.vsCMAccessPrivate);
                cmAttribute = (CodeAttribute2)cmClass.AddAttribute
                ("NewAttribute", "AttributeValue", -1);
            }
            else
            {
                MessageBox.Show("Cannot continue - no filecodemodel 
                available.");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("ERROR: " + ex);
        }
    }
}

public FileCodeModel2 GetFileCodeModel(DTE2 applicationObject)
{
    // Returns the FileCodeModel object of the active 
    // window.
    TextWindow txtWin = 
    (TextWindow)applicationObject.ActiveWindow.Object;
    FileCodeModel2 fcm2;
    if (txtWin != null)
    {
        try
        {
             fcm2 = (FileCodeModel2)txtWin.Parent.
             ProjectItem.FileCodeModel;
             return fcm2;
        }
        catch (Exception ex)
        {
             MessageBox.Show("ERROR: " + ex);
             return null;
        }
    }
    else
        return null;
}

See Also

Reference

EnvDTE80 Namespace

Other Resources

How to: Compile and Run the Automation Object Model Code Examples

Discovering Code by Using the Code Model (Visual Basic)

Discovering Code by Using the Code Model (Visual C#)