Partager via


Removing Type Mappings

You can remove redundant type mappings in two ways. You can either overwrite the type mapping with a new entry, or you can explicitly remove the type mapping programmatically.

Note

Existing type mappings will only be overwritten if both the interface name and the key string of the new mapping match those of the existing mapping. If the key string is null (not specified), the new type mapping will replace any existing type mapping with a matching interface name and a null key string.

To remove a type mapping from the current service locator instance, you should first use the SharePoint Service Locator to get a reference to a Microsoft.Practices.SharePoint.Common.ServiceLocation.IServiceLocatorConfig interface. The IServiceLocatorConfig object provides two methods that you can use to remove type mappings.

The RemoveTypeMapping method allows you to remove specific type mappings from the service locator. To remove a type mapping that was created without a key string, you must pass a null value as a parameter to the RemoveTypeMapping method.

IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();

IServiceLocatorConfig typeMappings =  
serviceLocator.GetInstance<IServiceLocatorConfig>();

typeMappings.RemoveTypeMapping<IService1>(null);

If the type mapping you want to remove includes a key string, pass the key string as a parameter to the RemoveTypeMapping method.

typeMappings.RemoveTypeMapping<IService1>("alternate");

Alternatively, you may wish to remove every type mapping associated with a particular interface name. In this case, you can use the RemoveTypeMappings method.

typeMappings.RemoveTypeMappings<IService1>();