Export (0) Print
Expand All

Resolving All Objects of a Particular Type

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Unity Application Block information can be found at the Unity Application Block site.

This scenario explores how you can retrieve a list of concrete instances of registered objects from a Unity container using the ResolveAll method when you have registered mappings for a type and differentiated them by name.

In this scenario, you retrieve an enumerable list of objects from the container. The list includes all objects registered with the specified type, and with a name that identifies the mapping or registration. You can then iterate through the list to locate an object or to display a list of the objects.

To retrieve a list of object instances from the container based on named registrations, use the ResolveAll method and specify a value for the object type (the registration type). The ResolveAll method returns an IEnumerable generic List of the non-default (named) registered types that you can iterate through in code to examine each object. Notice that it does not include in the list objects registered as the default (un-named) mapping for the specified type. If no registrations or mappings match the type you specify, the ResolveAll method returns null (in C#) or Nothing (in Visual Basic) Mapping names are simple strings, but are case-sensitive.


Ff650015.note(en-us,PandP.10).gifNote:
The API for the Unity container contains both generic and non-generic overloads of most of the methods, so that you can use it with languages that do not support the Generics syntax. The examples in this topic use the generic overloads. For more information about the non-generic overloads, see Unity Application Block Methods.


To retrieve a list of registered objects for a specified registered type

  1. Using a reference to the container, call the ResolveAll method and specify the required object type (the type specified when registering the object). The following code assumes you have registered one or more named mappings for the type IMyObject.
    IEnumerable<IMyObject> objects = myContainer.ResolveAll<IMyObject>();
    
    
    Ff650015.note(en-us,PandP.10).gifNote:
    If the container does not contain any named (non-default) mappings for the specified type, it will return null (in C#) or Nothing (in Visual Basic).

  2. Iterate over the list performing any required tasks with each object in the list. The following code assumes that the registered non-default mappings for IMyObject return objects of type MyRealObject.
    foreach (IMyObject foundObject in objects)
    {
      // convert the object reference to the "real" type
      MyRealObject theObject = foundObject as MyRealObject;
      if (null != theObject)
      // work with the object
      {
        Console.WriteLine(theObject.SomeProperty);
      }
    }
    
    

For more information about the techniques discussed in this scenario, see the following topics:


Show:
© 2014 Microsoft