Question

i have a method in a Usercontrol with this code

        public bool GetActiveDocument(ref EnvDTE.Document doc)
    {
        try
        {
            var dte = (DTE2)GetService(typeof(SDTE));
            doc = dte.ActiveDocument;
            if (doc == null)
            {
                MessageBox.Show("There isn't any file open in the Code Editor");
                return false;
            }
            else return true;
        }
        catch (Exception)
        {
            MessageBox.Show("There was a problem getting the actual file, verify if it is open");
            return false;
        }
    }

I want to move this method to a "Proxy" class that is going to act as an intermediary between Visual Studio and my application. The problem is that GetService only return the active document correctly if it is call inside the control. When i move that method to the Proxy class, GetService doesn't have definition. I search that this method comes from ComponentModel.Component, so i made the Proxy class derive from Component. Everything compiles OK but always when i ask for the active document an exception occurs. I thing that i'm not understanding well how works the GetService() method. Please help with this.

Was it helpful?

Solution

Component.GetService invokes IServiceProvider.GetService on the ISite instance assigned to the component's Site property (assuming there is one). You shouldn't need to make your proxy instance inherit from Component, but you will need to give it access to the ISite/IServiceProvider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top