-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathMarshalByValueComponent.cs
More file actions
170 lines (155 loc) · 5.97 KB
/
MarshalByValueComponent.cs
File metadata and controls
170 lines (155 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
namespace System.ComponentModel
{
/// <summary>
/// Provides the base implementation for <see cref='System.ComponentModel.IComponent'/>,
/// which is the base class for all components in Win Forms.
/// </summary>
[Designer("System.Windows.Forms.Design.ComponentDocumentDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.ComponentModel.Design.IRootDesigner, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
[DesignerCategory("Component")]
[TypeConverter(typeof(ComponentConverter))]
public class MarshalByValueComponent : IComponent, IServiceProvider
{
/// <summary>
/// Static hask key for the Disposed event. This field is read-only.
/// </summary>
private static readonly object s_eventDisposed = new object();
private ISite? _site;
private EventHandlerList? _events;
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.MarshalByValueComponent'/> class.
/// </summary>
public MarshalByValueComponent()
{
}
~MarshalByValueComponent() => Dispose(false);
/// <summary>
/// Adds an event handler to listen to the Disposed event on the component.
/// </summary>
public event EventHandler? Disposed
{
add => Events.AddHandler(s_eventDisposed, value);
remove => Events.RemoveHandler(s_eventDisposed, value);
}
/// <summary>
/// Gets the list of event handlers that are attached to this component.
/// </summary>
protected EventHandlerList Events => _events ??= new EventHandlerList();
/// <summary>
/// Gets or sets the site of the component.
/// </summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual ISite? Site
{
get => _site;
set => _site = value;
}
/// <summary>
/// Disposes of the resources (other than memory) used by the component.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
///
/// Disposes all the resources associated with this component.
/// If disposing is false then you must never touch any other
/// managed objects, as they may already be finalized. When
/// in this state you should dispose any native resources
/// that you have a reference to.
///
///
/// When disposing is true then you should dispose all data
/// and objects you have references to. The normal implementation
/// of this method would look something like:
///
/// <code>
/// public void Dispose() {
/// Dispose(true);
/// GC.SuppressFinalize(this);
/// }
///
/// protected virtual void Dispose(bool disposing) {
/// if (disposing) {
/// if (myobject != null) {
/// myobject.Dispose();
/// myobject = null;
/// }
/// }
/// if (myhandle != IntPtr.Zero) {
/// NativeMethods.Release(myhandle);
/// myhandle = IntPtr.Zero;
/// }
/// }
///
/// ~MyClass() {
/// Dispose(false);
/// }
/// </code>
///
/// For base classes, you should never override the Finalizer (~Class in C#)
/// or the Dispose method that takes no arguments, rather you should
/// always override the Dispose method that takes a bool.
///
/// <code>
/// protected override void Dispose(bool disposing) {
/// if (disposing) {
/// if (myobject != null) {
/// myobject.Dispose();
/// myobject = null;
/// }
/// }
/// if (myhandle != IntPtr.Zero) {
/// NativeMethods.Release(myhandle);
/// myhandle = IntPtr.Zero;
/// }
/// base.Dispose(disposing);
/// }
/// </code>
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
lock (this)
{
_site?.Container?.Remove(this);
((EventHandler?)_events?[s_eventDisposed])?.Invoke(this, EventArgs.Empty);
}
}
}
/// <summary>
/// Gets the container for the component.
/// </summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual IContainer? Container => _site?.Container;
/// <summary>
/// Gets the implementer of the <see cref='System.IServiceProvider'/>.
/// </summary>
public virtual object? GetService(Type service) => _site?.GetService(service);
/// <summary>
/// Gets a value indicating whether the component is currently in design mode.
/// </summary>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual bool DesignMode => _site?.DesignMode ?? false;
/// <summary>
/// Returns a <see cref='string'/> containing the name of the <see cref='System.ComponentModel.Component'/> , if any. This method should not be
/// overridden. For
/// internal use only.
/// </summary>
public override string? ToString()
{
ISite? s = _site;
if (s != null)
return s.Name + " [" + GetType().FullName + "]";
else
return GetType().FullName;
}
}
}