Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ServiceContainer.cs
Go to the documentation of this file.
2
4
6{
7 private sealed class ServiceCollection<T> : Dictionary<Type, T>
8 {
10 {
11 public bool Equals(Type x, Type y)
12 {
13 return x.IsEquivalentTo(y);
14 }
15
16 public int GetHashCode(Type obj)
17 {
18 return obj.FullName.GetHashCode();
19 }
20 }
21
23
28 }
29
31
33
34 private static readonly Type[] s_defaultServices = new Type[2]
35 {
38 };
39
41
42 protected virtual Type[] DefaultServices => s_defaultServices;
43
45
47 {
48 }
49
54
59
60 public virtual void AddService(Type serviceType, object serviceInstance, bool promote)
61 {
62 if (promote)
63 {
64 IServiceContainer container = Container;
65 if (container != null)
66 {
68 return;
69 }
70 }
71 if (serviceType == null)
72 {
73 throw new ArgumentNullException("serviceType");
74 }
75 if (serviceInstance == null)
76 {
77 throw new ArgumentNullException("serviceInstance");
78 }
79 if (!(serviceInstance is ServiceCreatorCallback) && !serviceInstance.GetType().IsCOMObject && !serviceType.IsInstanceOfType(serviceInstance))
80 {
82 }
84 {
85 throw new ArgumentException(System.SR.Format(System.SR.ErrorServiceExists, serviceType.FullName), "serviceType");
86 }
88 }
89
91 {
92 AddService(serviceType, callback, promote: false);
93 }
94
95 public virtual void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
96 {
97 if (promote)
98 {
99 IServiceContainer container = Container;
100 if (container != null)
101 {
102 container.AddService(serviceType, callback, promote);
103 return;
104 }
105 }
106 if (serviceType == null)
107 {
108 throw new ArgumentNullException("serviceType");
109 }
110 if (callback == null)
111 {
112 throw new ArgumentNullException("callback");
113 }
115 {
116 throw new ArgumentException(System.SR.Format(System.SR.ErrorServiceExists, serviceType.FullName), "serviceType");
117 }
118 Services[serviceType] = callback;
119 }
120
121 public void Dispose()
122 {
123 Dispose(disposing: true);
124 }
125
126 protected virtual void Dispose(bool disposing)
127 {
128 if (!disposing)
129 {
130 return;
131 }
133 _services = null;
134 if (services == null)
135 {
136 return;
137 }
138 foreach (object value in services.Values)
139 {
140 if (value is IDisposable)
141 {
142 ((IDisposable)value).Dispose();
143 }
144 }
145 }
146
147 public virtual object? GetService(Type serviceType)
148 {
149 object value = null;
151 for (int i = 0; i < defaultServices.Length; i++)
152 {
153 if (serviceType != null && serviceType.IsEquivalentTo(defaultServices[i]))
154 {
155 value = this;
156 break;
157 }
158 }
159 if (value == null && serviceType != null)
160 {
162 }
164 {
166 if (value != null && !value.GetType().IsCOMObject && !serviceType.IsInstanceOfType(value))
167 {
168 value = null;
169 }
171 }
172 if (value == null && _parentProvider != null)
173 {
175 }
176 return value;
177 }
178
180 {
182 }
183
184 public virtual void RemoveService(Type serviceType, bool promote)
185 {
186 if (promote)
187 {
188 IServiceContainer container = Container;
189 if (container != null)
190 {
192 return;
193 }
194 }
195 if (serviceType == null)
196 {
197 throw new ArgumentNullException("serviceType");
198 }
200 }
201}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
static readonly EmbeddedTypeAwareTypeComparer s_serviceTypeComparer
ServiceContainer(IServiceProvider? parentProvider)
void AddService(Type serviceType, ServiceCreatorCallback callback)
virtual ? object GetService(Type serviceType)
virtual void AddService(Type serviceType, object serviceInstance, bool promote)
virtual void RemoveService(Type serviceType, bool promote)
void AddService(Type serviceType, object serviceInstance)
virtual void AddService(Type serviceType, ServiceCreatorCallback callback, bool promote)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ErrorServiceExists
Definition SR.cs:82
static string ErrorInvalidServiceInstance
Definition SR.cs:80
Definition SR.cs:7
virtual bool IsEquivalentTo([NotNullWhen(true)] Type? other)
Definition Type.cs:1029
void AddService(Type serviceType, object serviceInstance)
object? GetService(Type serviceType)
delegate? object ServiceCreatorCallback(IServiceContainer container, Type serviceType)