0

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.

mjsr
  • 7,410
  • 18
  • 57
  • 83

1 Answers1

1

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.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • Thank you that works. I'm still doesn't understand well, but calling the GetService with a referece of the ISite is all that I need. My doubts is why when i put the cursor over the original GetService method the Intellisense tells me that is Component.GetService what i'm actually call and not IServiceProvider.GetService? – mjsr Jan 14 '11 at 16:10
  • The implementation of Component.GetService invokes the GetService method of the Site property. Intellisense shows documentation, not implementation. If you're interested in the details of the Component.GetService implementation, you might want to grab a copy of Reflector (http://www.red-gate.com/products/dotnet-development/reflector/). – Nicole Calinoiu Jan 14 '11 at 17:02
  • i follow your advise about .net reflector. I then look the implementation of the GetService and is exactly like you say, its the object that implement ISite the final responsibility to call the GetService() method. What impressive tool, how they manage to rebuild the code is impressive. I'm still feel that i don't have a real understanding of the component model, but this really help in the road of enlightenment, :). I'm going to continue searching about the topic. – mjsr Jan 14 '11 at 18:05