Component


Although you can implement IComponent to create a component, it is much easier to inherit Component because it provides a default implementation of IComponent. This approach will be used in the examples in this chapter. By inheriting Component, your class automatically fulfills the rules necessary to make a .NET-compatible component.

The Component class defines only the default constructor. Normally, you won’t construct a Component object directly, since the main use of Component is as a base class for components that you create.

Component defines two public properties. The first is Container, shown here:

 public IContainer Container { get; }

Container obtains the container that holds the invoking component. Null is returned if the component is not contained. Container is set by the container, and not by the component.

The second property is Site, which is defined by IComponent. It is implemented as a virtual property by Component, as shown here:

 public virtual ISite Site {get; set; }

It obtains or sets the ISite object linked to the component. Site is null if the component is not held in a container. Site is set by the container and not by the component.

Component defines two public methods. The first is an override of ToString( ). The second is the Dispose( ) method. It has two forms. The first is shown here:

 public void Dispose( )

It frees any resources used by the invoking component. This method implements the Dispose( ) method specified by the IDisposable interface. To release a component and its resources, the client will call this version of Dispose( ).

The second form of Dispose( ) is shown next:

 protected virtual public void Dispose(bool how)

If how is true, this version frees both managed and unmanaged resources used by the invoking component. If how is false, it frees only unmanaged resources. Because this version of Dispose( ) is protected, it cannot be called by client code. Instead, it is called by the first version of Dispose( ). In other words, calling the first version of Dispose( ) generates a call to Dispose(bool).

In general, your component will override Dispose(bool) when it holds resources that need to be freed when the component is no longer needed. If your component does not hold any resources, then the default implementation of Dispose(bool) supplied by Component is sufficient.

Component inherits the class MarshalByRefObject, which is used when a component is instantiated outside its local application environment, such as when a component is created in another process or on a different computer connected via a network. For data, such as method arguments and return values, to be exchanged, a mechanism must be in place that defines how the data is sent. By default, information is marshaled by value, but by inheriting MarshalByRefObject, data is marshaled by reference. Thus, a C# component marshals data by reference.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net