CodeAttribute2.Value 属性
设置或获取代码特性的数据。
命名空间: EnvDTE80
程序集: EnvDTE80(在 EnvDTE80.dll 中)
语法
声明
Property Value As String
Get
Set
string Value { get; set; }
property String^ Value {
String^ get ();
void set (String^ value);
}
abstract Value : string with get, set
function get Value () : String
function set Value (value : String)
属性值
类型:System.String
表示代码特性数据的字符串值。
实现
备注
如果特性为 name(someval, 2) 形式,则该值为 someval, 2。
提示
代码特性参数值,在赋值之后,Visual Studio 不将这些值保留在内存中,因此,当以后对代码特性参数进行更新时,这些值可能会有效,也可能会无效。 即,在以后访问参数时可能会返回 E_FAIL 或者一个完全不同的值。 (但是,影响元素子级的任何内容都不会出现此问题。)
由于存在这种非确定性行为,因此您应当在更改参数值之前先检索它。 例如,如果在代码中设置了代码特性参数(如 myAttrArg.Value = """a first value"""),则应当在更新它之前显式引用它(如 myAttrArg = myAttr.Arguments.Item("first value")),然后为它赋一个新值 myAttrArg.Value = """a second value"""。 这样做可以确保更改正确的参数。
同样,在进行某些类型的编辑之后,代码模型元素(如类、结构、函数、特性、委托等)的值可能是非确定性的,这意味着不能指望它们的值总是保持不变。 有关更多信息,请参见 使用代码模型查找代码 (Visual Basic) 中的“代码模型元素的值可能会更改”一节。
示例
下面的示例在当前类中创建一个新的命名空间和特性,并列出该特性的一些属性。
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);
}
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。