为自己开发的扩展获取正确的VS2017实例 [英] Get right instance of VS2017 for self developed extension

查看:49
本文介绍了为自己开发的扩展获取正确的VS2017实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Visual Studio扩展,以使用从代码窗口的右键单击上下文菜单中调用的自定义命令来替换当前活动.cs文件中的文本.

I'm developing a Visual Studio Extension to replace text in the current active .cs file using a custom command that is invoked from the right click context menu in the Code Window.

到目前为止,访问文档的工作正常,但是如果我启动多个VS2017实例,那么我期望在新实例中进行的更改将在第一个打开的实例中进行.

Accessing the document works so far, but if I start more than one instance of VS2017, then changes which I expect to be done in the new instance are made in the first opened instance.

无论打开了多少实例,是否都有可能获得正确的实例以仅访问当前活动文档?

Is there a possibility to get the right instance to access only the current active Document no matter how many instances are open?

目前,我使用以下代码获取实例:

At the moment I get the instance with following code:

dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal
    .GetActiveObject("VisualStudio.DTE.15.0");    

有人知道如何解决这个问题吗?

Does anybody have an idea how to solve this?

推荐答案

正如 Carlos Quintero 所述,您应该得到通过使用他的示例的DTE对象.

As Carlos Quintero already said, you should get the DTE Object by using his example.

让我们说您的扩展名是YourExtension:

Lets say your extension name is YourExtension:

就我而言,我在YourExtension.cs中添加了一个属性

In my case, I added a Property in my YourExtension.cs

 public EnvDTE.DTE DTEObject { get; set; }

然后在YourExtension Package .cs中,您可以在初始化程序包后立即获得所需的DTEObject:

Then in YourExtensionPackage.cs you can get the desired DTEObject right after your package got initialized:

protected override void Initialize ()
{
  YourExtension.Initialize (this);
  base.Initialize ();
  YourExtension.Instance.DTEObject = (EnvDTE.DTE)base.GetService (typeof (Microsoft.VisualStudio.Shell.Interop.SDTE));
}

现在,您可以在扩展程序中使用DTEObject并通过GetObject获取任何对象.以我为例,我正在获取VersionControlEx的当前实例.

Now you can work with the DTEObject within your extension and get any Object via GetObject. In My case for example I'm getting the current instance of the VersionControlEx.

这篇关于为自己开发的扩展获取正确的VS2017实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆