CodeAttribute2.Collection 属性

定义

获取 CodeAttribute2 对象的集合。

public:
 property EnvDTE::CodeElements ^ Collection { EnvDTE::CodeElements ^ get(); };
[System.Runtime.InteropServices.DispId(2)]
public EnvDTE.CodeElements Collection { [System.Runtime.InteropServices.DispId(2)] [System.Runtime.InteropServices.TypeLibFunc(1024)] get; }
[<System.Runtime.InteropServices.DispId(2)>]
[<get: System.Runtime.InteropServices.DispId(2)>]
[<get: System.Runtime.InteropServices.TypeLibFunc(1024)>]
member this.Collection : EnvDTE.CodeElements
Public ReadOnly Property Collection As CodeElements

属性值

CodeElements

CodeAttribute2 对象的集合。

实现

属性

示例

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

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

注解

备注

在赋值后,代码属性参数值不会保留在内存中 Visual Studio ,因此,在对 code 特性参数进行后续更新时,可能会也可能无效。 也就是说,后续参数访问可能返回 E_FAIL 或完全不同的值。 但 (影响元素的子级的任何内容,则不会出现此问题。 )

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

此外,在进行某些类型的编辑之后,代码模型元素(例如类、结构、函数、特性、委托等)的值可能是不确定的,这意味着无法依赖于它们的值始终保持不变。 有关详细信息,请参阅 "代码模型元素值在 使用代码模型查找代码时 可能发生变化" (Visual Basic) "。

适用于