2010-09-24 75 views
4

我有一個Visual Studio 2010擴展,一個.vsix文件。我可以通過打印dte_instance.Solution.Fullname來確認我特定的Visual Studio實例的DTE實例。但是對於我的DTE2實例,它似乎給我提供了有關錯誤的Visual Studio實例的信息。Visual Studio 2010擴展(.vsix),獲取DTE2實例

這裏是工作流程:Visual Studio開發IDE打開,有擴展代碼。啓動該項目,這將導致啓動一個新的Visual Studio實例,並在其中安裝擴展。點擊運行下面的代碼我的菜單按鈕(在新的IDE):

DTE dte; 
DTE2 dte2, dte2Macros; 
dte = (DTE)GetService(typeof(DTE)); 
dte2 = (DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0"); 
dte2Macros = (DTE2)dte2.MacrosIDE; 

//this returns what I expect, the solution name in the newer IDE. 
MessageBox.Show("solution name: " + dte.Solution.FullName); 

//code to get the startup project from MSDN 
//http://msdn.microsoft.com/en-us/library/ms228782.aspx 
SolutionBuild2 sb = (SolutionBuild2)dte2.Solution.SolutionBuild; 
string msg = ""; 
Int32 configs = sb.SolutionConfigurations.Count; 
foreach (String item in (Array)sb.StartupProjects) 
{ 
    msg += item; 
} 

//this returns a project from the development IDE, the one I don't want. 
System.Windows.Forms.MessageBox.Show("startup project is: " + msg); 
Project startupProject = dte2.Solution.Item(msg); 

我發現了幾個引用與connect()方法獲取DTE2對象中的插件,但我無法找到一個類似的回調用於擴展。

問題:如何獲得擴展在IDE中執行的IDE的DTE2實例?

回答

11

嘗試this,它採用了進口的服務提供商,或者只是使用Package.GetGlobalService

DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;

+0

完美地工作,謝謝。 – Adam 2010-09-25 00:10:07

+0

我有完全相同的問題,而且我使用的方法與他完全相同,但是當我嘗試解決方案時,我得到「名稱'包'在當前上下文中不存在」。它的正確用法是什麼? – Marcelo 2010-10-14 14:20:17

+0

您需要對Microsoft.VisualStudio.Shell.10.dll的引用,以及對Microsoft.VisualStudio.Shell的using。 – 2010-10-14 15:14:27

0

我有一些機器Package.GetGlobalService(typeof(DTE))返回null問題。 現在我在Package的Initialize()方法中使用(DTE2)base.GetService(typeof(DTE))(它類似於加載項的connect()方法)。