0

I have created an object that uses the DTE to access files in a Visual Studio Solution and parse them to extract information to drive new features I am implementing. This object also uses DTE Events to detect when the contents of the Solution changes. For this reason the object is scoped in the VSPackage. However, I need the information the object collects inside a IOleCommandTarget where the features are implemented. I have tried looking for a way to pass this object from the VSPackage to the IOleCommandTarget but so far I have come up empty. I have tried scoping the object in the IOleCommandTarget but this does not work because the DTE events will not trigger due to the scope or Garbage Collection, I think.

Here is my IOleCommandTarget provider:

internal class VerilogCommandAndControlHandlerProvider : IVsTextViewCreationListener
{
    [Import]
    internal IVsEditorAdaptersFactoryService AdapterService = null;
    [Import]
    internal ICompletionBroker CompletionBroker { get; set; }
    [Import]
    internal SVsServiceProvider ServiceProvider { get; set; }

    public void VsTextViewCreated(IVsTextView textViewAdapter)
    {
        ITextView textView = AdapterService.GetWpfTextView(textViewAdapter);
        if (textView == null)
            return;

        DTE dte = ((DTE)ServiceProvider.GetService(typeof(DTE)));

        Func<VerilogCommandAndControlHandler> createCommandHandler = delegate () { return new VerilogCommandAndControlHandler(textViewAdapter, textView, dte, this); };
        textView.Properties.GetOrCreateSingletonProperty(createCommandHandler);
    }

}
Neil Pittman
  • 359
  • 1
  • 3
  • 12
  • Could you not pass this helper class that detects solution changes and collection information to the IOleCommandTarget implementation via constructor argument? – Suresh Aug 15 '16 at 20:02
  • Sure, the piece I am missing is how to access this helper class inside the IOleCommandTarget provider to pass it in the constructor. – Neil Pittman Aug 15 '16 at 20:56
  • 1
    Regarding the garbage collection problem of DTE events, I normally have a reference to the corresponding events object (e.g BuildEvents, SolutionEvents) stored in a private field in package class. That way, those events objects don't get collected by the GC. – Suresh Aug 15 '16 at 21:35
  • 1
    Looks like you are using MEF, may be you could `Export` your helper class and `Import` it in `VerilogCommandAndControlHandlerProvider` class. – Suresh Aug 15 '16 at 21:37

0 Answers0