mscorlib(4.0.0.0) API with additions
Component.cs
2 
3 namespace System.ComponentModel
4 {
6  [ComVisible(true)]
7  [ClassInterface(ClassInterfaceType.AutoDispatch)]
8  [DesignerCategory("Component")]
10  {
11  private static readonly object EventDisposed = new object();
12 
13  private ISite site;
14 
15  private EventHandlerList events;
16 
20  protected virtual bool CanRaiseEvents => true;
21 
22  internal bool CanRaiseEventsInternal => CanRaiseEvents;
23 
26  protected EventHandlerList Events
27  {
28  get
29  {
30  if (events == null)
31  {
32  events = new EventHandlerList(this);
33  }
34  return events;
35  }
36  }
37 
40  [Browsable(false)]
42  public virtual ISite Site
43  {
44  get
45  {
46  return site;
47  }
48  set
49  {
50  site = value;
51  }
52  }
53 
56  [Browsable(false)]
58  public IContainer Container
59  {
60  get
61  {
62  return site?.Container;
63  }
64  }
65 
69  [Browsable(false)]
71  protected bool DesignMode
72  {
73  get
74  {
75  return site?.DesignMode ?? false;
76  }
77  }
78 
80  [Browsable(false)]
81  [EditorBrowsable(EditorBrowsableState.Advanced)]
82  public event EventHandler Disposed
83  {
84  add
85  {
86  Events.AddHandler(EventDisposed, value);
87  }
88  remove
89  {
90  Events.RemoveHandler(EventDisposed, value);
91  }
92  }
93 
95  ~Component()
96  {
97  Dispose(disposing: false);
98  }
99 
101  public void Dispose()
102  {
103  Dispose(disposing: true);
104  GC.SuppressFinalize(this);
105  }
106 
110  protected virtual void Dispose(bool disposing)
111  {
112  if (disposing)
113  {
114  lock (this)
115  {
116  if (site != null && site.Container != null)
117  {
118  site.Container.Remove(this);
119  }
120  if (events != null)
121  {
122  ((EventHandler)events[EventDisposed])?.Invoke(this, EventArgs.Empty);
123  }
124  }
125  }
126  }
127 
131  protected virtual object GetService(Type service)
132  {
133  return site?.GetService(service);
134  }
135 
138  public override string ToString()
139  {
140  ISite site = this.site;
141  if (site != null)
142  {
143  return site.Name + " [" + GetType().FullName + "]";
144  }
145  return GetType().FullName;
146  }
147  }
148 }
string Name
Gets or sets the name of the component associated with the T:System.ComponentModel....
Definition: ISite.cs:34
virtual ISite Site
Gets or sets the T:System.ComponentModel.ISite of the T:System.ComponentModel.Component.
Definition: Component.cs:43
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
EditorBrowsableState
Specifies the browsable state of a property or method from within an editor.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
void AddHandler(object key, Delegate value)
Adds a delegate to the list.
object GetService(Type serviceType)
Gets the service object of the specified type.
Definition: __Canon.cs:3
Provides a simple list of delegates. This class cannot be inherited.
override string ToString()
Returns a T:System.String containing the name of the T:System.ComponentModel.Component,...
Definition: Component.cs:138
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: EventArgs.cs:9
EventHandlerList Events
Gets the list of event handlers that are attached to this T:System.ComponentModel....
Definition: Component.cs:27
void RemoveHandler(object key, Delegate value)
Removes a delegate from the list.
Provides functionality for containers. Containers are objects that logically contain zero or more com...
Definition: IContainer.cs:7
static readonly EventArgs Empty
Provides a value to use with events that do not have event data.
Definition: EventArgs.cs:13
void Dispose()
Releases all resources used by the T:System.ComponentModel.Component.
Definition: Component.cs:101
virtual bool CanRaiseEvents
Gets a value indicating whether the component can raise an event.
Definition: Component.cs:20
delegate void EventHandler(object sender, EventArgs e)
Represents the method that will handle an event that has no event data.
Provides the base implementation for the T:System.ComponentModel.IComponent interface and enables obj...
Definition: Component.cs:9
Provides functionality required by all components.
Definition: IComponent.cs:13
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
EventHandler Disposed
Occurs when the component is disposed by a call to the M:System.ComponentModel.Component....
Definition: Component.cs:83
void Remove(IComponent component)
Removes a component from the T:System.ComponentModel.IContainer.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.ComponentModel.Component and optionally release...
Definition: Component.cs:110
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
virtual object GetService(Type service)
Returns an object that represents a service provided by the T:System.ComponentModel....
Definition: Component.cs:131
bool??? DesignMode
Gets a value that indicates whether the T:System.ComponentModel.Component is currently in design mode...
Definition: Component.cs:72
DesignerSerializationVisibility
Specifies the visibility a property has to the design-time serializer.
Encapsulates zero or more components.
Definition: Container.cs:7
ClassInterfaceType
Identifies the type of class interface that is generated for a class.
IContainer Container
Gets the T:System.ComponentModel.IContainer associated with the T:System.ComponentModel....
Definition: ISite.cs:19
Provides functionality required by sites.
Definition: ISite.cs:7
bool DesignMode
Determines whether the component is in design mode when implemented by a class.
Definition: ISite.cs:27
Enables access to objects across application domain boundaries in applications that support remoting.