不编写插件时获取当前的 EnvDTE 或 IServiceProvider

2024-01-05

我正在编写一些设计时代码。我想使用这个片段:(发现here http://social.msdn.microsoft.com/Forums/br/netfxbcl/thread/7548f267-2b73-4996-a9c5-eaa707037add)

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),但没有帮助。

在互联网上查找时,我发现您需要一个 IServiceProvider 来调用它。

但所有显示如何获取 IServiceProvider 的示例都使用 EnvDTE。

因此,要获取当前的 EnvDTE,我需要 IServiceProvider。但要获得 IServiceProvider,我需要一个 EnvDTE。 (我的水桶有个洞……)

所以,这是我的问题:

在普通的WPF应用程序中,我怎样才能获得当前实例环境DTE?

注意:我并不是在寻找任何旧的 EnvDTE 实例。我需要一个适用于我当前的 Visual Studio 实例的实例(我一次运行 3-4 个 Visual Studio 实例。)


这个问题有您正在寻找的答案。

在 Visual C# 2010 中获取 DTE2 对象的引用 https://stackoverflow.com/questions/4724381/get-the-reference-of-the-dte2-object-in-visual-c-sharp-2010

具体来说

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

这是代码:

Usings:

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

Method:

[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;
}

正如另一个答案所示,这在调试时不起作用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不编写插件时获取当前的 EnvDTE 或 IServiceProvider 的相关文章

  • 从另一个命令 Handle() 方法中调用命令

    嗨 我正在使用简易注射器 https simpleinjector orgDI 库并一直在关注一些关于围绕命令模式设计的架构模型的非常有趣的材料 同时 在我的架构的命令方面 https cuttingedge it blogs steven
  • 分布式环境中的 MS Team Foundation Server - 提示所需技巧

    是否有人在地理分布的团队中使用 Team Foundation Server 我们在英国 尝试与澳大利亚的团队合作 但我们发现这非常困难 我们主要的两个问题是 事情正在向我们检查 而我们无需询问最新情况 即使使用代理 大多数事情也需要一段时
  • C# 5 async/await 线程机制感觉不对?

    为什么让调用线程进入异步方法直到内部 等待 一旦调用异步方法就生成一个线程 这不是更干净吗 这样您就可以确定异步方法会立即返回 您不必担心在异步方法的早期阶段没有做任何昂贵的事情 我倾向于知道某个方法是否要在 我的 线程上执行代码 不管是堵
  • 英文日期差异

    接近重复 如何计算相对时间 https stackoverflow com questions 11 how do i calculate relative time 如何在 C 中计算某人的年龄 https stackoverflow c
  • 通过 Tab 键浏览 XML 文档字段

    In VB NET you can move through the fields in the XML member documentation with the Tab key 这在 C 中不起作用 还有其他方法吗 除了用鼠标将光标放在
  • 本地用户名不允许使用字符

    目前 如果我尝试将本地用户 jim bob 添加到 Windows 7 系统 如预期的那样 将显示以下错误对话框 其中显示用户名的所有非法字符 是否有一个快速的 net 方法可以调用并返回相同的字符 例如 Path GetInvalidPa
  • 如何并行运行一组函数并等待完成结果?

    我需要同时异步运行一组繁重的函数并将结果填充到列表中 这是伪代码 List
  • string.IsNullOrEmpty() 与 string.NotNullOrEmpty()

    我很好奇是否有开发人员更频繁地使用 string IsNullOrEmpty 来表示负数而不是正数 e g if string IsNullOrEmpty 我99 的时间都是这样使用这个方法的 对此的设计决定是什么 因为 IsNullOrE
  • Task.Delay 值得取消吗?

    我最近使用取消模式重新实现了一大堆异步 WCF 服务方法 我在很多地方都看到过这种模式的描述 您可以在其中等待Task WhenAny在已启动的任务和 Task Delay 上 当然 现有任务是不可取消的 但这有望在以后的版本中得到解决 就
  • 当前有哪些 USB 设备(友好名称)连接到 PC?

    我可以获得当前连接到计算机的设备列表吗 我检查了this https stackoverflow com q 3331043 75500 and this https stackoverflow com questions 3685615
  • Breeze + NHibernate 多对一关系,避免指定关系键

    以下多对一映射正在运行 取自 NorthBreeze public partial class UserRole public virtual long ID get set public virtual long UserId get s
  • 实施 INotifyPropertyChanged - 是否存在更好的方法?

    微软应该实施一些快速的措施INotifyPropertyChanged 就像在自动属性中一样 只需指定 get set notify 我认为这样做很有意义 或者这样做有什么并发症吗 我们自己可以在我们的属性中实现类似 通知 的功能吗 有没有
  • 如何使用 MSBuild 构建 Visual Studio 安装项目

    我正在尝试使用 MSBuild 使用以下文章构建 Visual Studio 安装项目 http msdn microsoft com en us library ms404859 aspx https web archive org we
  • 比较 LINQ to SQL 中的两个日期

    我有一个数据库 其中有一个名为会议的表 会议日期使用以下格式存储在此表中 May 2nd 2011 例如 格式为5 2 2011 我的要求是获取两个日期 例如 2011 年 4 月 25 日和 2011 年 5 月 2 日 之间的会议 并编
  • 我的 WPF 应用程序未触发 MainWindow_Loaded

    我目前正在关注Pluralsight C Fundamentals Part 1并在Classes and Objects视频部分指导我在 Visual Studio 中创建一个新的 WPF 应用程序并填写代码 这导致以下结果 namesp
  • 试图使用加密来混淆我的项目打破了它

    我试图尝试不同的混淆选项 为了做到这一点 我首先尝试了加密货币 以下是我遵循的步骤 打开加密向导并选择一些选项 选择我的解决方案文件 完成向导后 我看到有些 Dll 被很好地混淆了 但我的项目现在无法构建 我注意到的两件事是 我的文件夹中有
  • 如何组合||条件语句中的运算符[重复]

    这个问题在这里已经有答案了 代替 if foo 1 foo 5 foo 9 我喜欢将它们组合起来 类似于以下内容 这不起作用 if foo 1 5 9 那可能吗 不幸的是不是 你最好的选择是创建一个扩展方法 public static bo
  • Gridview 错误:对 Bind 的调用格式不正确

    我有以下 gridview 代码
  • 如何使用完全空的类型使 Activator.CreateInstance 运行速度减慢约 20 倍

    Given NET 程序集命名expression host NET 程序集命名CreateInstanceTest CreateInstanceTest 在其配置文件中启用 NetFx40 LegacySecurityPolicy exp
  • 替换全局热键

    我有一个位于托盘中的应用程序 我想定义多个热键来触发我的程序中的事件 我从 AaronLS 在这个问题中的出色回答中找到了灵感 使用C 设置全局热键 https stackoverflow com a 27309185 3064934 如果

随机推荐