我正在编写一些设计时代码。我想使用这个片段:(发现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(使用前将#替换为@)