How to: Retrieve an Interface Implementation

The following procedure demonstrates how to retrieve an implementation of a specified interface from the SharePoint Service Locator.

To retrieve an interface implementation from the SharePoint Service Locator

  1. Add assembly references to Microsoft.Practices.SharePoint.Common.dll and Microsoft.Practices.ServiceLocation.dll.

  2. Add the following using statements to the top of your source code file.

    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.SharePoint.Common.ServiceLocation;
    
  3. Declare an object of type IServiceLocator and set it to the value of the SharePointServiceLocator.GetCurrent() method.

    IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
    
  4. Call the IServiceLocator.GetInstance method. Set the type parameter to the interface for which you want to retrieve the registered implementation.

    IService1 service1 = serviceLocator.GetInstance<IService1>();