Globals.VariablePersists 属性
VariablePersists 属性适用于多种类型的 Globals 对象。对于 DTE.Globals 对象,它获取或设置的值指示变量是否由环境保留以及是否在环境的会话之间可用。对于 Solution.Globals 对象,它获取或设置的值指示变量是否由环境保留、是否在环境的各会话之间以及解决方案的加载和卸载之间可用。对于 Project.Globals 对象,它获取或设置的值指示变量是否由环境保留在项目文件中。
命名空间: EnvDTE
程序集: EnvDTE(在 EnvDTE.dll 中)
语法
声明
Property VariablePersists ( _
VariableName As String _
) As Boolean
bool this[
string VariableName
] { get; set; }
property bool VariablePersists[String^ VariableName] {
bool get (String^ VariableName);
void set (String^ VariableName, bool value);
}
abstract VariablePersists : bool with get, set
JScript 不支持索引属性。
参数
- VariableName
类型:System.String
必选。表示要保留的变量名。
属性值
类型:System.Boolean
一个指示某个变量是否存在的布尔值。如果某个变量存在,则 VariablePersists 返回 true,否则返回 false。
备注
虽然全局变量在 Visual Studio 的会话内始终保持不变,但 VariablePersists 可使这些变量在会话之间保持不变。
说明 |
---|
若要将变量与特定解决方案保存在一起,请使用 DTE.Solution.Globals。 |
如果变量不存在,则 VariablePersists 返回 false。
对于 Solution 对象 (Solution.Globals),每当保存解决方案时都会保存数据。修改 Globals 对象会使解决方案文件成为标记为编辑过的(或“已更新的”)解决方案文件。对于 DTE 对象 (DTE.Globals),关闭 Visual Studio 环境或保存解决方案时都会保存数据。在这两种情况中,数据存储在 User Profiles 目录中的解决方案 (.sln) 文件或结构化存储文件中。
关闭环境或执行 Save All 时,保存所有的全局值。如果 VariablePersists 与 DTE 对象相关联,则在 Visual Studio 环境的用户选项目录中保存值。
如果全局变量与 Solution 对象相关联,则在解决方案 (.sln) 文件中保存值。只要环境写 .sln 文件,就保存值。
任何已保存的变量将覆盖先前保存的值。若要从保存的文件中移除某变量,请将其 VariablePersists 设置为 false。在下一次 Save 操作期间,环境将移除该变量的值。
说明 |
---|
VariableValue 名称不能包含空格。如果包含空格,则会遇到错误,即值不在预期范围内。 |
示例
Sub OnAddinLoaded(ByVal dte As DTE)
' Count the number of times an add-in is loaded
' and store the value in the solution.
Dim globals As Globals
globals = dte.Solution.Globals
If globals.VariableExists("AddinLoadCounter") Then
' The counter has already been set, so increment it.
Dim int32 As System.Int32
int32 = System.Int32.Parse(CStr(globals("AddinLoadCounter")))
int32 += 1
globals("AddinLoadCounter") = int32.ToString()
Else
' Counter has never been set, so create and initialize it.
globals("AddinLoadCounter") = 1.ToString()
globals.VariablePersists("AddinLoadCounter") = True
End If
MsgBox("This add-in has been loaded: " & _
globals.VariableValue("AddinLoadCounter") & " times.")
End Sub
void OnAddinLoaded(_DTE applicationObject)
{
// Count the number of times an add-in is loaded
// and store the value in the solution.
Globals globals;
globals = applicationObject.Solution.Globals;
if(globals.get_VariableExists("AddinLoadCounter"))
{
// The counter has already been set, so increment it.
System.Int32 int32;
int32 = System.Int32.Parse((string)
globals["AddinLoadCounter"]);
int32++;
globals["AddinLoadCounter"] = int32.ToString();
}
else
{
// Counter has never been set, so create and initialize it.
globals["AddinLoadCounter"] = 1.ToString();
globals.set_VariablePersists("AddinLoadCounter", true);
}
System.Windows.Forms.MessageBox.Show("This add-in has been loaded:
" + globals.VariableValue["AddinLoadCounter"] + " times.");
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。