获取当前EnvDTE或IServiceProvider的时候无需编码一个插件插件、时候、EnvDTE、IServiceProvider

由网友(浮森)分享简介:我编码了一些设计时code。我想利用这个片段:(发现here)I am coding up some design time code. I want to use this snippet: (Found here)var dte = (EnvDTE.DTE) GetService(typeof(EnvDTE....

我编码了一些设计时code。我想利用这个片段:(发现here)

I am coding up some design time code. I want to use this snippet: (Found here)

var dte = (EnvDTE.DTE) GetService(typeof(EnvDTE.DTE));
if (dte != null)
{
    var solution = dte.Solution;
    if (solution != null)
    {
        string baseDir = Path.GetDirectoryName(solution.FullName);
    }
}

问题是,这不会编译。 (GetService的不是一个已知的方法调用),我尝试添加Microsoft.VisualStudio.Shell(和Microsoft.VisualStudio.Shell.10.0),但它并没有帮助。

Problem is that this does not compile. (GetService is not a known method call) I tried adding Microsoft.VisualStudio.Shell (and Microsoft.VisualStudio.Shell.10.0) but it did not help.

在环顾四周,我发现在互联网上,你需要一个IServiceProvider的调用此。

In looking around on the internet I found that you need a IServiceProvider to call this.

但这一切,显示了如何获取的IServiceProvider使用EnvDTE的例子。

But all the examples that show how to get an IServiceProvider use a EnvDTE.

所以,为了获得当前EnvDTE我需要的IServiceProvider。但要获得的IServiceProvider我需要一个EnvDTE。 (有一个在我的水桶一个洞......)

So, to get the current EnvDTE I need IServiceProvider. But to get an IServiceProvider I need an EnvDTE. (There is a hole in my bucket...)

所以,这里是我的问题:

So, here is my question:

在一个正常的WPF应用程序,如何才能得到EnvDTE的的当前实例的?

In a normal WPF Application, how can I get the current instance of EnvDTE?

注:我不找EnvDTE任何旧实例。我需要一个为我目前的Visual Studio实例(我运行Visual Studio 3-4实例的时间。)

NOTE: I am not looking for any old instance of EnvDTE. I need the one for my current Visual Studio instance (I run 3-4 instances of Visual Studio at a time.)

推荐答案

这个问题有答案,你正在寻找。

This question has the answer to which you're looking.

Get在DTE2对象在Visual C#参考2010

特别

http://stackoverflow.com/a/4724924/858142

下面是code:的

Here is the code:

Usings:

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using EnvDTE;
using Process = System.Diagnostics.Process;

方法:

[DllImport("ole32.dll")]
private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc);
[DllImport("ole32.dll")]
private static extern void GetRunningObjectTable(int reserved,
                                                 out IRunningObjectTable prot);
internal static DTE GetCurrent()
{
   //rot entry for visual studio running under current process.
   string rotEntry = String.Format("!VisualStudio.DTE.10.0:{0}",
                                    Process.GetCurrentProcess().Id);
   IRunningObjectTable rot;
   GetRunningObjectTable(0, out rot);
   IEnumMoniker enumMoniker;
   rot.EnumRunning(out enumMoniker);
   enumMoniker.Reset();
   IntPtr fetched = IntPtr.Zero;
   IMoniker[] moniker = new IMoniker[1];
   while (enumMoniker.Next(1, moniker, fetched) == 0)
   {
       IBindCtx bindCtx;
       CreateBindCtx(0, out bindCtx);
       string displayName;
       moniker[0].GetDisplayName(bindCtx, null, out displayName);
       if (displayName == rotEntry)
       {
           object comObject;
           rot.GetObject(moniker[0], out comObject);
           return (DTE)comObject;
       }
   }
   return null;
}

由于对方的回答表明,这并不在调试工作。

As the other answer indicates, this does not work while debugging.

阅读全文

相关推荐

最新文章