CodeAttribute2 接口

更新:2007 年 11 月

定义代码元素的属性。

命名空间:  EnvDTE80
程序集:  EnvDTE80(在 EnvDTE80.dll 中)

语法

声明
<GuidAttribute("35187E2A-E5F6-4F89-A4CE-DA254640855B")> _
Public Interface CodeAttribute2 _
    Implements CodeAttribute
用法
Dim instance As CodeAttribute2
[GuidAttribute("35187E2A-E5F6-4F89-A4CE-DA254640855B")]
public interface CodeAttribute2 : CodeAttribute
[GuidAttribute(L"35187E2A-E5F6-4F89-A4CE-DA254640855B")]
public interface class CodeAttribute2 : CodeAttribute
public interface CodeAttribute2 extends CodeAttribute

备注

CodeAttribute2 对象表示一个与代码元素关联的 COM 元数据属性。您可以通过对适当的对象使用 AddAttribute 方法添加新属性,并通过对适当的对象使用 Delete 方法删除属性。可以使用该对象获取和设置代码属性的值。

说明:

在进行某些类型的编辑之后,代码模型元素(如类、结构、函数、属性、委托等)的值可能是非确定性的,这意味着不能指望它们的值总是保持不变。有关更多信息,请参见使用代码模型查找代码 (Visual Basic) 中的“代码模型元素的值可能会更改”一节。

示例

// 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;
}

另请参见

参考

CodeAttribute2 成员

EnvDTE80 命名空间

其他资源

如何:编译和运行自动化对象模型代码示例

使用代码模型查找代码 (Visual Basic)

使用代码模型查找代码 (Visual C#)