获取在Visual C#2010 DTE2对象的引用 [英] Get the reference of the DTE2 object in Visual C# 2010

查看:257
本文介绍了获取在Visual C#2010 DTE2对象的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要得到当前解决方案的参考,使用用C#DTE2对象在Visual Studio 2010。

I want to get a reference to the current solution, using the DTE2 object with C# in Visual Studio 2010.

我第一次尝试以下code:

I first tried the following code:

var dte = Marshal.GetActiveObject("VisualStudio.DTE.10.0") as EnvDTE80.DTE2;

但是,当我打开2解决方案,并且此code是在第一溶液时,得到不参考对当前的解决方案,但对I加载的最后溶液的参考。我需要当前的解决方案...

But when I open 2 solutions, and this code is in the first solution, I get NOT a reference to the current solution, but a reference to the last solution I loaded. I need the current solution...

在互联网上搜索,我发现在<一下面的解决方案href=\"http://stackoverflow.com/questions/2336818/how-do-you-get-the-current-solution-directory-from-a-vspackage\">How你从VSPackage的获取当前解决方案目录:

Searching on the internet, I found the following solution in How do you get the current solution directory from a VSPackage?:

// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));

但是,当我用这个,我DTE对象始终是NULL。

But when I use this, my dte object is always NULL.

所以,我如何才能在VS2010我目前的解决方案对象使用C#.NET的框架4.0?

So how do I get to my current solution object in VS2010 using C# on .net framework 4.0?

推荐答案

在一些广泛的搜索和努力,我终于使用被添加到MSDN页面的评论得到了答案:<一href=\"http://msdn.microsoft.com/en-us/library/ms228755.aspx\">http://msdn.microsoft.com/en-us/library/ms228755.aspx

After some extensive searching and trying i finally got the answer using the comment that was added to the MSDN page: http://msdn.microsoft.com/en-us/library/ms228755.aspx

我添加了一个静态类,我的C#项目:

I added a static class to my c# project:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using EnvDTE80;

[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 DTE2 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 (EnvDTE80.DTE2)comObject;
        }
     }
     return null;
  }

和那个我想要访问当前IDE点:

And at the point that I want to access the current IDE:

var dte = CurrentIde.GetCurrent();
var sol = dte.Solution;

但要记住....这code将不会在调试过程中工作! code的开始串rotEntry ...该生产线具有的Process.GetCurrentProcess一个调用来获取当前进程的ID。

But remember.... This code will NOT work during debugging!!! The line of code starting with string rotEntry... has a call to the Process.GetCurrentProcess to get the ID of the current process.

在调试的一些功能在我的插件(使用MME HTTP://mme.$c$cplex.com/ )我打电话,需要当前IDE的方法。我调用插件方法ConsoleApp测试。在获取当前的IDE来看,currentprocess是不是IDE,但ConsoleApp.vshost.exe。所以,我的code没有调试过程中工作,但构建插件和安装此插件后,没有工作。

While debugging some functionality in my addin (using MME http://mme.codeplex.com/) I call a method that needs the current IDE. I test this with a ConsoleApp that calls the addin method. At the point of getting the current IDE, the currentprocess is NOT the IDE, but the ConsoleApp.vshost.exe. So my code did not work during debugging, but DID work after building the addin and installing this addin.

这篇关于获取在Visual C#2010 DTE2对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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