Share via


Communication Between Containers and Components

A container is a nexus for communication between the client application and the components it contains. An application may obtain references to components within the container without knowing the actual name or identity of the component. The component may also interact with the client application through the container in a variety of ways.

The container object exposes the components it contains through its Components property. This property is an indexed property that returns an IComponent reference object. Components are tracked on a first-in, first-out basis, and are accessible through the index using the following syntax:

Imports System.ComponentModel
Dim MyContainer As Container
Dim xComponent as IComponent
xComponent = MyContainer.Components(0)
using System.ComponentModel;
Container MyContainer = new Container();
IComponent xComponent;
xComponent = MyContainer.Components[0];

Components may be added to a container with or without a name. If you know the name of the component you are referring to, you may also get the reference through the container using that name, as shown in the following example:

xComponent = MyContainer.Components("myComponent")
xComponent = MyContainer.Components["myComponent"];

Note that the Components property returns an IComponent reference, and will not allow you to access methods and properties of the component that are not implemented by that interface.

The component communicates with the container primarily through its Site property. Through Site, the component can obtain a reference to the IContainer interface implemented by the container, as shown below:

Dim myComponent As New Component()
Dim myIContainer as IContainer
myIContainer = myComponent.Site.Container
Component myComponent = new Component();
IContainer myIContainer;
myIContainer = myComponent.Site.Container;

The same reference is also returned by the Container property. This can be thought of as a shortcut: The reference is still provided through the ISite object, but not explicitly in this case.

The component can also get services from the container (if they are supplied) by calling IServiceProvider.GetService Method. This method returns an object of the specified type, as shown below:

Dim myComponent As Component
Dim myWidget As Widget
Dim serviceObject As Object
' This returns an object of type Widget that is supplied by the container.
serviceObject = myComponent.Site.GetService(GetType(Widget))
myWidget = CType(serviceObject, Widget)
Component myComponent = new Component();
Widget myWidget;
object serviceObject;
// This returns an object of type Widget that is supplied by the container.
serviceObject = myComponent.Site.GetService(System.Type.GetType("CommunicateCS.Widget"));
myWidget = (Widget)serviceObject;

In order to receive an object via GetService, it must be implemented in an inherited container class. The GetService method of the Container class should be overridden and code to supply the service object implemented.

See Also

Tasks

How to: Create Component Containers

How to: Extend Component Containers

Concepts

Containers, Sites, and Components