CodeAttribute2.Value 属性

定义

设置或获取代码特性的数据。

public:
 property System::String ^ Value { System::String ^ get(); void set(System::String ^ value); };
public:
 property Platform::String ^ Value { Platform::String ^ get(); void set(Platform::String ^ value); };
[System.Runtime.InteropServices.DispId(32)]
public string Value { [System.Runtime.InteropServices.DispId(32)] get; [System.Runtime.InteropServices.DispId(32)] set; }
[<System.Runtime.InteropServices.DispId(32)>]
[<get: System.Runtime.InteropServices.DispId(32)>]
[<set: System.Runtime.InteropServices.DispId(32)>]
member this.Value : string with get, set
Public Property Value As String

属性值

String

表示代码特性数据的字符串值。

实现

属性

示例

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

Sub ValueExample(ByVal dte As DTE2)  

    ' Before running this example, open a code document from a project  
    ' and place the insertion point inside a class definition.  
    Try  
        ' Retrieve the CodeClass at the insertion point.  
        Dim sel As TextSelection = _  
            CType(dte.ActiveDocument.Selection, TextSelection)  
        Dim cls As CodeClass = CType(sel.ActivePoint.CodeElement( _  
            vsCMElement.vsCMElementClass), CodeClass)  

        ' Enumerate the CodeClass's attributes.  
        Dim attrs As String = ""  
        Dim attr As CodeAttribute  
        For Each attr In cls.Attributes  
            attrs &= attr.Name & "(" & attr.Value & ")" & vbCrLf  
        Next  

        MsgBox(cls.Name & " has the following attributes:" & _  
            vbCrLf & vbCrLf & attrs)  
    Catch ex As Exception  
        MsgBox(ex.Message)  
    End Try  

End Sub  
public void ValueExample(DTE2 dte)  
{  
    // Before running this example, open a code document from a project  
    // and place the insertion point inside a class definition.  
    try  
    {  
        // Retrieve the CodeClass at the insertion point.  
        TextSelection sel =   
            (TextSelection)dte.ActiveDocument.Selection;  
        CodeClass cls = (CodeClass)sel.ActivePoint.get_CodeElement(  
            vsCMElement.vsCMElementClass);  

        // Enumerate the CodeClass's attributes.  
        string attrs = "";  
        foreach (CodeAttribute attr in cls.Attributes)  
        {  
            attrs += attr.Name + "(" + attr.Value + ")" +   
                Environment.NewLine;  
        }  

        MessageBox.Show(cls.Name + " has the following attributes:" +   
            Environment.NewLine + Environment.NewLine + attrs);  
    }  
    catch (Exception ex)  
    {  
        MessageBox.Show(ex.Message);  
    }  
}  

注解

如果属性采用格式 name(someval, 2) ,则值为 someval, 2

备注

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

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

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

适用于