System.ComponentModel.Component Class

Assembly: System.dll
Namespace: System.ComponentModel
Summary
Provides the base implementation for the IComponent interface and enables object-sharing between applications.
C# Syntax:
public class Component : MarshalByRefObject, IComponent, IDisposable
Remarks
Component is the base class for all components in the common language runtime which marshal by reference. Component is remotable and derives from MarshalByRefObject. Component provides an implementation of IComponent. The MarshalByValueComponent provides an implementation of IComponent which marshals by value.

You can host a Component in any object that implements the IContainer interface, and can query and get services from its container. The container creates an ISite for each Component it contains. The container uses the site to manage the Component and is used by the Component to communicate with its container.

It is recommended that a Component release resources explicitly by calls to its Component.Dispose method, without waiting for automatic memory management through an implicit call to Object.Finalize. When a Container is disposed, all components within the Container are also disposed.

See also:
System.ComponentModel Namespace | IComponent | IContainer | Container | ISite

System.ComponentModel.Component Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Public Properties
Container Read-only

Gets the IContainer that contains the Component.
Site Read-write

Gets or sets the ISite of the Component.
Public Methods
CreateObjRef
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.CreateObjRef


Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
Dispose Overloaded:
Dispose()

Releases all resources used by the Component.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.GetLifetimeService


Retrieves the current lifetime service object that controls the lifetime policy for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
InitializeLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.InitializeLifetimeService


Obtains a lifetime service object to control the lifetime policy for this instance.
ToString Overridden:
Returns a String containing the name of the Component, if any. This method should not be overridden. For internal use only.
Public Events
Disposed Adds an event handler to listen to the Component.Disposed event on the component.
Protected Properties
DesignMode Read-only

Gets a value that indicates whether the Component is currently in design mode.
Events Read-only

Gets the list of event handlers that are attached to this Component.
Protected Methods
Dispose Overloaded:
Dispose(bool disposing)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.
Finalize Overridden:
GetService Returns an object that represents a service provided by the Component or by its Container.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.ComponentModel.Component Member Details

ctor #1
Summary:
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public Component();

Return to top


Property: Container (read-only)
Summary
Gets the IContainer that contains the Component.
C# Syntax:
public IContainer Container {get;}
Remarks
The components in a container are tracked in a first-in, first-out list, which also defines the order of the components within the container. The last component added is the last component in the list.
See also:
IContainer | Container

Return to top


Property: DesignMode (read-only)
Summary
Gets a value that indicates whether the Component is currently in design mode.
C# Syntax:
protected bool DesignMode {get;}
Remarks
The design mode indicator is stored in the ISite; therefore, if the Component does not have an ISite associated with it, this property is always false.
See also:
IContainer | Container | ISite

Return to top


Property: Events (read-only)
Summary
Gets the list of event handlers that are attached to this Component.
C# Syntax:
protected EventHandlerList Events {get;}
Remarks
For more information about handling events, see the conceptual topic at MSDN: eventsoverview.
See also:
EventHandlerList

Return to top


Property: Site (read-write)
Summary
Gets or sets the ISite of the Component.
C# Syntax:
public virtual ISite Site {get; set;}
Implements:
IComponent.Site
Remarks
A Component will have an ISite if it has been added to an IContainer and the IContainer assigns an ISite to it. The IContainer is responsible to assign the ISite to the Component. Changing the value of the component's ISite does not necessarily change the name of the site the Component is assigned to. It is strongly suggested that the setting of the Component.Site property only be done by an IContainer.

The property value is null if the Component is removed from its IContainer. Assigning null to this property does not necessarily remove the Component from the IContainer.

A Component might or might not have a name. If a Component is given a name, the name must be unique among other Component objects within its IContainer. The ISite stores the name of the Component; therefore, you can only name a Component if it has an ISite associated with it.

See also:
IContainer | Container | ISite

Return to top


Method: CreateObjRef(
   Type requestedType
)
Inherited
See base class member description: System.MarshalByRefObject.CreateObjRef

Summary
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual ObjRef CreateObjRef(
   Type requestedType
);
Parameters:

requestedType

The Type of the object that the new ObjRef will reference.

Return Value:
Information required to generate a proxy.
Exceptions
Exception Type Condition
RemotingException This instance is not a valid remoting object.

Return to top


Overloaded Method: Dispose()
Summary
Releases all resources used by the Component.
C# Syntax:
public void Dispose();
Implements:
IDisposable.Dispose
Remarks
Calling Component.Dispose allows the resources used by the Component to be reallocated for other purposes. For more information about Component.Dispose, see the conceptual topic at MSDN: cleaningupunmanagedresources.
See also:
IContainer | Container

Return to top


Overloaded Method: Dispose(
   bool disposing
)
Summary
Releases the unmanaged resources used by the Component and optionally releases the managed resources.
C# Syntax:
protected virtual void Dispose(
   bool disposing
);
Parameters:

disposing

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks
This method is called by the public Dispose method and the Object.Finalize method.Dispose() invokes the protected Dispose(Boolean) method with the disposing parameter set to true. Object.Finalize invokes Dispose with disposing set to false.

When the disposing parameter is true, this method releases all resources held by any managed objects that this Component references. This method invokes the Dispose() method of each referenced object.



Notes to inheritors: Dispose can be called multiple times by other objects. When overriding Dispose(Boolean), be careful not to reference objects that have been previously disposed of in an earlier call to Dispose. For more information about how to implement Dispose(Boolean), see the conceptual topic at MSDN: implementingdisposemethod.

For more information about Dispose and Object.Finalize, see the conceptual topic at MSDN: cleaningupunmanagedresources and the conceptual topic at MSDN: overridingfinalizemethod.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: Finalize()
C# Syntax:
~Component();
Remarks
Calls Component.Dispose with disposing set to false to free unmanaged resources.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.GetLifetimeService

Summary
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
public object GetLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance.
Remarks
For more information about lifetime services, see the LifetimeServices class.

Return to top


Method: GetService(
   Type service
)
Summary
Returns an object that represents a service provided by the Component or by its Container.
C# Syntax:
protected virtual object GetService(
   Type service
);
Parameters:

service

A service provided by the Component.

Return Value:
An Object that represents a service provided by the Component.

This value is null if the Component does not provide the specified service.

Remarks
This method can be overridden by a derived class.
See also:
IContainer | Container | ISite | IServiceProvider

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InitializeLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.InitializeLifetimeService

Summary
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual object InitializeLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LifetimeServices.LeaseManagerPollTime property.
Remarks
For more information about lifetime services, see the LifetimeServices class.
Example
The following code example demonstrates creating a lease.
 public class MyClass : MarshalByRefObject
 {
   public override Object InitializeLifetimeService()
   {
     ILease lease = (ILease)base.InitializeLifetimeService();
     if (lease.CurrentState == LeaseState.Initial)
     {
          lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
          lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
           lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
     }
       return lease;
   }
 }

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: ToString()
Summary
Returns a String containing the name of the Component, if any. This method should not be overridden. For internal use only.
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
public override string ToString();
Return Value:
A String containing the name of the Component, if any.

null if the Component is unnamed.

Remarks
A Component may or may not have a name. If a Component is given a name, the name is unique among other Component objects within its IContainer.

The ISite stores the Component 's name. A Component can only have a ISite if it has been added to an IContainer and the IContainer assigns an ISite to it.

See also:
String | IContainer | Container | ISite

Return to top


Event: Disposed
Summary
Adds an event handler to listen to the Component.Disposed event on the component.
C# Syntax:
public event EventHandler Disposed;
Remarks
When you create a Component.Disposed delegate, you identify the method that handles the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about the event handler delegates, see the conceptual topic at MSDN: eventsdelegates.
See also:
EventHandler

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.