Share via


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 不一定要與增益集有關,它們可以與巨集或其他 Automation 用戶端相關聯。

範例

如需如何執行增益集程式碼的詳細資訊,請參閱 HOW TO:編譯和執行 Automation 物件模型程式碼範例

下列範例會將全域變數加入至開啟方案,並顯示方案中的所有全域變數。

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 命名空間

其他資源

HOW TO:編譯和執行 Automation 物件模型程式碼範例