CodeAttribute2.Collection 属性

获取 CodeAttribute2 对象的集合。

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

语法

声明
ReadOnly Property Collection As CodeElements
CodeElements Collection { get; }
property CodeElements^ Collection {
    CodeElements^ get ();
}
abstract Collection : CodeElements with get
function get Collection () : CodeElements

属性值

类型:CodeElements
CodeAttribute2 对象的集合。

备注

备注

代码特性参数值,在赋值之后,Visual Studio 不将这些值保留在内存中,因此,当以后对代码特性参数进行更新时,这些值可能会有效,也可能会无效。即,在以后访问参数时可能会返回 E_FAIL 或者一个完全不同的值。(但是,影响元素子级的任何内容都不会出现此问题。)

由于存在这种非确定性行为,因此您应当在更改参数值之前先检索它。例如,如果在代码中设置了代码特性参数(如 myAttrArg.Value = """a first value"""),则应当在更新它之前显式引用它(如 myAttrArg = myAttr.Arguments.Item("first value")),然后为它赋一个新值 myAttrArg.Value = """a second value"""。这样做可以确保更改正确的参数。

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

示例

下面的示例在当前类中创建一个新的命名空间和特性,并列出该特性的一些属性。

public void CreateClassAndAttrib(DTE2 applicationObject)
{
    // Before running, load or create a project.
    FileCodeModel2 fcm2 = GetFileCodeModel(applicationObject);
    CodeAttribute2 cmAttribute;
    CodeClass2 cmClass;
    String msg = null;

    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);
                msg += "Attribute count: " + 
                cmAttribute.Collection.Count + Environment.NewLine;
                msg += "Document name: " + 
                cmAttribute.DTE.ActiveDocument.Name;
            }
            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;
}

.NET Framework 安全性

请参阅

参考

CodeAttribute2 接口

EnvDTE80 命名空间

其他资源

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

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

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