mscorlib(4.0.0.0) API with additions
Container.cs
2 
3 namespace System.ComponentModel
4 {
6  [HostProtection(SecurityAction.LinkDemand, SharedState = true)]
8  {
9  private class Site : ISite, IServiceProvider
10  {
11  private IComponent component;
12 
13  private Container container;
14 
15  private string name;
16 
17  public IComponent Component => component;
18 
19  public IContainer Container => container;
20 
21  public bool DesignMode => false;
22 
23  public string Name
24  {
25  get
26  {
27  return name;
28  }
29  set
30  {
31  if (value == null || name == null || !value.Equals(name))
32  {
33  container.ValidateName(component, value);
34  name = value;
35  }
36  }
37  }
38 
39  internal Site(IComponent component, Container container, string name)
40  {
41  this.component = component;
42  this.container = container;
43  this.name = name;
44  }
45 
46  public object GetService(Type service)
47  {
48  if (!(service == typeof(ISite)))
49  {
50  return container.GetService(service);
51  }
52  return this;
53  }
54  }
55 
56  private ISite[] sites;
57 
58  private int siteCount;
59 
60  private ComponentCollection components;
61 
62  private ContainerFilterService filter;
63 
64  private bool checkedFilter;
65 
66  private object syncObj = new object();
67 
70  public virtual ComponentCollection Components
71  {
72  get
73  {
74  lock (syncObj)
75  {
76  if (components == null)
77  {
78  IComponent[] array = new IComponent[siteCount];
79  for (int i = 0; i < siteCount; i++)
80  {
81  array[i] = sites[i].Component;
82  }
83  components = new ComponentCollection(array);
84  if (filter == null && checkedFilter)
85  {
86  checkedFilter = false;
87  }
88  }
89  if (!checkedFilter)
90  {
92  checkedFilter = true;
93  }
94  if (filter != null)
95  {
96  ComponentCollection componentCollection = filter.FilterComponents(components);
97  if (componentCollection != null)
98  {
99  components = componentCollection;
100  }
101  }
102  return components;
103  }
104  }
105  }
106 
108  ~Container()
109  {
110  Dispose(disposing: false);
111  }
112 
117  public virtual void Add(IComponent component)
118  {
119  Add(component, null);
120  }
121 
130  public virtual void Add(IComponent component, string name)
131  {
132  lock (syncObj)
133  {
134  if (component != null)
135  {
136  ISite site = component.Site;
137  if (site == null || site.Container != this)
138  {
139  if (sites == null)
140  {
141  sites = new ISite[4];
142  }
143  else
144  {
145  ValidateName(component, name);
146  if (sites.Length == siteCount)
147  {
148  ISite[] destinationArray = new ISite[siteCount * 2];
149  Array.Copy(sites, 0, destinationArray, 0, siteCount);
150  sites = destinationArray;
151  }
152  }
153  site?.Container.Remove(component);
154  ISite site2 = CreateSite(component, name);
155  sites[siteCount++] = site2;
156  component.Site = site2;
157  components = null;
158  }
159  }
160  }
161  }
162 
167  protected virtual ISite CreateSite(IComponent component, string name)
168  {
169  return new Site(component, this, name);
170  }
171 
173  public void Dispose()
174  {
175  Dispose(disposing: true);
176  GC.SuppressFinalize(this);
177  }
178 
182  protected virtual void Dispose(bool disposing)
183  {
184  if (disposing)
185  {
186  lock (syncObj)
187  {
188  while (siteCount > 0)
189  {
190  ISite site = sites[--siteCount];
191  site.Component.Site = null;
192  site.Component.Dispose();
193  }
194  sites = null;
195  components = null;
196  }
197  }
198  }
199 
203  protected virtual object GetService(Type service)
204  {
205  if (!(service == typeof(IContainer)))
206  {
207  return null;
208  }
209  return this;
210  }
211 
214  public virtual void Remove(IComponent component)
215  {
216  Remove(component, preserveSite: false);
217  }
218 
219  private void Remove(IComponent component, bool preserveSite)
220  {
221  lock (syncObj)
222  {
223  if (component != null)
224  {
225  ISite site = component.Site;
226  if (site != null && site.Container == this)
227  {
228  if (!preserveSite)
229  {
230  component.Site = null;
231  }
232  int num = 0;
233  while (true)
234  {
235  if (num >= siteCount)
236  {
237  return;
238  }
239  if (sites[num] == site)
240  {
241  break;
242  }
243  num++;
244  }
245  siteCount--;
246  Array.Copy(sites, num + 1, sites, num, siteCount - num);
247  sites[siteCount] = null;
248  components = null;
249  }
250  }
251  }
252  }
253 
256  protected void RemoveWithoutUnsiting(IComponent component)
257  {
258  Remove(component, preserveSite: true);
259  }
260 
268  protected virtual void ValidateName(IComponent component, string name)
269  {
270  if (component == null)
271  {
272  throw new ArgumentNullException("component");
273  }
274  if (name == null)
275  {
276  return;
277  }
278  int num = 0;
279  while (true)
280  {
281  if (num >= Math.Min(siteCount, sites.Length))
282  {
283  return;
284  }
285  ISite site = sites[num];
286  if (site != null && site.Name != null && string.Equals(site.Name, name, StringComparison.OrdinalIgnoreCase) && site.Component != component)
287  {
289  if (inheritanceAttribute.InheritanceLevel != InheritanceLevel.InheritedReadOnly)
290  {
291  break;
292  }
293  }
294  num++;
295  }
296  throw new ArgumentException(SR.GetString("DuplicateComponentName", name));
297  }
298  }
299 }
Provides a base class for the container filter service.
static AttributeCollection GetAttributes(Type componentType)
Returns a collection of attributes for the specified type of component.
string Name
Gets or sets the name of the component associated with the T:System.ComponentModel....
Definition: ISite.cs:34
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual void ValidateName(IComponent component, string name)
Determines whether the component name is unique for this container.
Definition: Container.cs:268
virtual void Remove(IComponent component)
Removes a component from the T:System.ComponentModel.Container.
Definition: Container.cs:214
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
static sbyte Min(sbyte val1, sbyte val2)
Returns the smaller of two 8-bit signed integers.
Definition: Math.cs:762
Definition: __Canon.cs:3
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.ComponentModel.Container, and optionally releas...
Definition: Container.cs:182
virtual ISite CreateSite(IComponent component, string name)
Creates a site T:System.ComponentModel.ISite for the given T:System.ComponentModel....
Definition: Container.cs:167
virtual void Add(IComponent component)
Adds the specified T:System.ComponentModel.Component to the T:System.ComponentModel....
Definition: Container.cs:117
InheritanceLevel
Defines identifiers for types of inheritance levels.
Indicates whether the component associated with this attribute has been inherited from a base class....
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides functionality for containers. Containers are objects that logically contain zero or more com...
Definition: IContainer.cs:7
IComponent Component
Gets the component associated with the T:System.ComponentModel.ISite when implemented by a class.
Definition: ISite.cs:12
Provides the base implementation for the T:System.ComponentModel.IComponent interface and enables obj...
Definition: Component.cs:9
virtual object GetService(Type service)
Gets the service object of the specified type, if it is available.
Definition: Container.cs:203
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
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
Provides information about the characteristics for a component, such as its attributes,...
void Remove(IComponent component)
Removes a component from the T:System.ComponentModel.IContainer.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
virtual ComponentCollection Components
Gets all the components in the T:System.ComponentModel.Container.
Definition: Container.cs:71
void RemoveWithoutUnsiting(IComponent component)
Removes a component from the T:System.ComponentModel.Container without setting P:System....
Definition: Container.cs:256
The exception that is thrown when one of the arguments provided to a method is not valid.
Defines a mechanism for retrieving a service object; that is, an object that provides custom support ...
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
Provides a read-only container for a collection of T:System.ComponentModel.IComponent objects.
Encapsulates zero or more components.
Definition: Container.cs:7
IContainer Container
Gets the T:System.ComponentModel.IContainer associated with the T:System.ComponentModel....
Definition: ISite.cs:19
Provides constants and static methods for trigonometric, logarithmic, and other common mathematical f...
Definition: Math.cs:10
Provides functionality required by sites.
Definition: ISite.cs:7
InheritanceLevel InheritanceLevel
Gets or sets the current inheritance level stored in this attribute.
virtual ComponentCollection FilterComponents(ComponentCollection components)
Filters the component collection.
void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
ISite Site
Gets or sets the T:System.ComponentModel.ISite associated with the T:System.ComponentModel....
Definition: IComponent.cs:18
void Dispose()
Releases all resources used by the T:System.ComponentModel.Container.
Definition: Container.cs:173
virtual void Add(IComponent component, string name)
Adds the specified T:System.ComponentModel.Component to the T:System.ComponentModel....
Definition: Container.cs:130