Solution2.Globals 属性

获取 Globals 对象,该对象包含可以保存在解决方案 (.sln) 文件、项目文件或用户的配置文件数据中的任何变量值。

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

语法

声明
ReadOnly Property Globals As Globals
    Get
Globals Globals { get; }
property Globals^ Globals {
    Globals^ get ();
}
abstract Globals : Globals
function get Globals () : Globals

属性值

类型:EnvDTE.Globals
一个 Globals 对象。

实现

_Solution.Globals

备注

外接程序在加载解决方案时可用。

Globals 无需与外接程序关联;它们还可能与宏或任何其他自动化客户端关联。

示例

有关如何运行此外接程序代码的信息,请参见如何:编译和运行自动化对象模型代码示例

下面的示例将全局变量添加到打开的解决方案,并显示解决方案中的所有全局变量。

Public Sub OnConnection(ByVal application As Object, _
 ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
 ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    GlobalsExample(_applicationObject)
End Sub

Sub GlobalsExample(ByVal dte As DTE2)
    ' This add-in adds a global variable to a solution and
    ' displays it.
    ' Open a solution in 
    ' Visual Studio before running this example.
    Try
        Dim soln As Solution2 =  _
        CType(_applicationObject.Solution, Solution2)
        Dim solnName As String = _
        System.IO.Path.GetFileNameWithoutExtension(soln.FullName)
        Dim globals As String = ""
        MsgBox("Adding global variable TempGlobal = ""TempValue""")
        soln.Globals.VariableValue("TempGlobal") = "TempValue"
        Dim names() As Object =  _
        CType(soln.Globals.VariableNames, Object())
        Dim name As String
        For Each name In names
            globals &= "    " & name & " = """ & _
            soln.Globals.VariableValue(name).ToString() & """" & vbCrLf
        Next
        MsgBox("Solution " & solnName & _
        " has the following global variables:" & _
        vbCrLf & vbCrLf & globals)
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub
//you will need to add this reference to your project as well
using System.Windows.Forms;
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst,
 ref System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    GlobalsExample((DTE2)_applicationObject);
}
public void GlobalsExample(DTE2 dte)
{
    // This add-in adds a global variable to the solution and 
    // displays it. 
    // Open a solution in 
    // Visual Studio before running this example.
    try
    {
        Solution2 soln = (Solution2)_applicationObject.Solution;
        string solnName =
 System.IO.Path.GetFileNameWithoutExtension(soln.FullName);
        MessageBox.Show
("Adding global variable TempGlobal = \"TempValue\"");
        soln.Globals["TempGlobal"] = "TempValue";
        object[] names = (object[])soln.Globals.VariableNames; 
        string globals = "";
        foreach (string name in names)
            globals += "    " + name + " = \"" 
+ soln.Globals[name].ToString() + "\"\n";
        MessageBox.Show("Solution " + solnName 
+ " has the following global variables:\n\n" + globals);
    }
    catch(SystemException ex)
    {
        MessageBox.Show("ERROR: " + ex);
    }
}

.NET Framework 安全性

请参见

参考

Solution2 接口

Globals 重载

EnvDTE80 命名空间

其他资源

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