How to get at EnvDTE from a plugin?
I need to access EnvDTE from my R#6/VS2010 plugin. I see some messages from a few years ago on how to reference EnvDTE in order to do this, but the technique doesn't seem to apply to the current APIs. Can someone outline what needs to be done here?
Thanks,
Jeremy
Please sign in to leave a comment.
It's not pretty, but you can get a reference to DTE2 directly through com. See http://stackoverflow.com/questions/4724381/get-the-reference-of-the-dte2-object-in-visual-c-sharp-2010 for how to accomplish this.
Have you tried simply injecting
by putting it as a constructor parameter in the component where you need it?Injecting didn't work for me:
ReplaceWithAutoComplete constuctor is never called. Is there other ways to get DTE? I need to execute commands. Is there other way to do this?
As of ReSharper 6.1 + 7.0, Action handlers cannot have dependencies injected. They require either a parameterless constructor, or a constructor with a single argument, which can be either the string action id that the handler is for, or an instance of ActionManager.
You can get various information such as solution, project and text control from the IDataContext parameter to the Update and Execute method (look for classes called DataConstants that contain readonly instances of a DataConstant that acts as a key). But unfortunately, DTE isn't available that way.
Instead, you need to call Shell.Instance.GetComponent<DTE>(). This can be called from your action handler's constructor, and should work fine (just checked on a quick plugin, and it gets a value correctly)
Thanks
Matt
I tried to get DTE in Action constructor, but even parametless constructor is never called by Resharper. So, I am getting DTE in Execute method. Unfortunetly I am getting an exception:
Could not find the component DTE in the chain of component containers.
Here is my code:
I feel like I am really close to solving this problem. Please help.
What versions of ReSharper and Visual Studio are you using? I successfully retrieved the DTE object in an action handler constructor with ReSharper 7 + VS2012.
The parameterless constructor must be called by ReSharper if Execute gets called. The only reason it isn't being called is if you have another constructor.
I am using VS2010 SP1 + ReSharper 7.0.1.1098.2760.
I referenced EnvDTE from:
c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\EnvDTE.dll
Runtime version: v1.0.3705
Version: 8.0.0.0
Also Execute gets called only if I remove parameterless constructor.
Hmm. I can't reproduce this. It looks like I'm using the same version of ReSharper, VS and EnvDTE.dll as you, and it all seems to work. Could you post a simple repro? You can email it to me at resharper-plugins@jetbrains.com
Matt
I created new plugin project from scratch for demo purposes. I can get DTE in new project and parameterless constructor is also called during initialization. I guess there is something wrong with my project, so problem is on my side. Thank you for your help.
It was a .NET framework version. I used 4.0. I switched to 3.5 and it fixed an issue.
Sorry for the late reply. This does (kinda) make sense. ReSharper plugins should really be .net 3.5 dlls, as ReSharper is still a .net 3.5 product. That is, it runs on the 2.0 CLR, to still support older versions of Visual Studio. I guess by having the plugin as a .net 4 assembly, it's resolving the wrong reference for envdte (perhaps it's looking in the v4 GAC rather than the v2 GAC) and it can't find the component.
Anyway, glad you got it sorted.
Matt
Matt,
Shell was removed in Resharper 9, so I can't get DTE using Shell.Instance.TryGetComponent<DTE>().
Do you know how I can get DTE in Resharper 9?
Viktar
Nevermind, I found it at JetBrains.ReSharper.Resources.Shell namespace.
Viktar
Where you able to get the DTE Viktar? I tried that code in R#9.2 with no luck.
JetBrains.ReSharper.Resources.Shell.Shell.Instance.TryGetComponent<DTE>() returns null for me.
I didn't upgraded to 9.2 yet. Shell.Instance.TryGetComponent<DTE>() works for me with Resharper 9.1.3.
What framework version your project targeting? I am using .NET 4.0. Previosly I used some other version and had a problem. Try to switch .NET Framework version, maybe now it has to be 4.5.
Viktar
No luck. I tried 4.0, 4.5, 4.5.1 and 4.6. I am using Visual Studio 2015 and testing in the VS hive.
Another issue might be that since DTE is a COM interface, and if it's referenced with Embed COM Interop Types set to true, the System.Type used to identify the DTE object is different to the System.Type used to register it in the first place. Try rebuilding with Embed COM Interop Types set to false.
Out of curiosity, what do you want to use DTE for? There might be a more ReSharper-y way of doing what you need.
Thanks Matt, it works now!
I'm using it to do code cleanup on TFS check in files. Not sure if this would be the best way to do this but these are my steps:
This was a solution I came up in R#8.2, it works most of the time and hangs up VS sometimes but I couldn't find any other way to get the list of check ins from R#
Not sure if in R#9.2 if there is a way to directly get the list of TFS check in files?
NOTE: sorry to piggy back on someone elses post.
I'm afraid I don't know anything about TFS check in. If there's a Visual Studio interface that can get the data you need, ReSharper should be able to get the interface.