Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReflectEventDescriptor.cs
Go to the documentation of this file.
5
7
9{
10 private Type _type;
11
12 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
13 private readonly Type _componentClass;
14
16
18
20
21 private bool _filledMethods;
22
24
25 public override Type EventType
26 {
27 get
28 {
30 return _type;
31 }
32 }
33
34 public override bool IsMulticast => typeof(MulticastDelegate).IsAssignableFrom(EventType);
35
36 public ReflectEventDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentClass, string name, Type type, Attribute[] attributes)
37 : base(name, attributes)
38 {
39 if (componentClass == null)
40 {
41 throw new ArgumentException(System.SR.Format(System.SR.InvalidNullArgument, "componentClass"));
42 }
43 if (type == null || !typeof(Delegate).IsAssignableFrom(type))
44 {
46 }
47 _componentClass = componentClass;
48 _type = type;
49 }
50
51 public ReflectEventDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentClass, EventInfo eventInfo)
52 : base(eventInfo.Name, Array.Empty<Attribute>())
53 {
54 _componentClass = componentClass ?? throw new ArgumentException(System.SR.Format(System.SR.InvalidNullArgument, "componentClass"));
55 _realEvent = eventInfo;
56 }
57
58 public ReflectEventDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType, EventDescriptor oldReflectEventDescriptor, Attribute[] attributes)
59 : base(oldReflectEventDescriptor, attributes)
60 {
61 _componentClass = componentType;
62 _type = oldReflectEventDescriptor.EventType;
63 if (oldReflectEventDescriptor is ReflectEventDescriptor reflectEventDescriptor)
64 {
65 _addMethod = reflectEventDescriptor._addMethod;
66 _removeMethod = reflectEventDescriptor._removeMethod;
67 _filledMethods = true;
68 }
69 }
70
71 public override void AddEventHandler(object component, Delegate value)
72 {
74 if (component == null)
75 {
76 return;
77 }
78 ISite site = MemberDescriptor.GetSite(component);
79 IComponentChangeService componentChangeService = null;
80 if (site != null)
81 {
82 componentChangeService = (IComponentChangeService)site.GetService(typeof(IComponentChangeService));
83 }
84 if (componentChangeService != null)
85 {
86 try
87 {
88 componentChangeService.OnComponentChanging(component, this);
89 }
90 catch (CheckoutException ex)
91 {
93 {
94 return;
95 }
96 throw;
97 }
98 componentChangeService.OnComponentChanging(component, this);
99 }
100 bool flag = false;
101 if (site != null && site.DesignMode)
102 {
103 if (EventType != value.GetType())
104 {
106 }
107 IDictionaryService dictionaryService = (IDictionaryService)site.GetService(typeof(IDictionaryService));
108 if (dictionaryService != null)
109 {
110 Delegate a = (Delegate)dictionaryService.GetValue(this);
111 a = Delegate.Combine(a, value);
112 dictionaryService.SetValue(this, a);
113 flag = true;
114 }
115 }
116 if (!flag)
117 {
118 MethodInfo addMethod = _addMethod;
119 object[] parameters = new Delegate[1] { value };
120 addMethod.Invoke(component, parameters);
121 }
122 componentChangeService?.OnComponentChanged(component, this, null, value);
123 }
124
125 protected override void FillAttributes(IList attributes)
126 {
127 FillMethods();
128 if (_realEvent != null)
129 {
131 }
132 else
133 {
136 }
137 base.FillAttributes(attributes);
138 }
139
140 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "currentReflectType is in _componentClass's hierarchy. Since _componentClass is annotated with All, this means currentReflectType is annotated with All as well.")]
141 private void FillEventInfoAttribute(EventInfo realEventInfo, IList attributes)
142 {
143 string name = realEventInfo.Name;
144 BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public;
145 Type type = realEventInfo.ReflectedType;
146 int num = 0;
147 while (type != typeof(object))
148 {
149 num++;
150 type = type.BaseType;
151 }
152 if (num <= 0)
153 {
154 return;
155 }
156 type = realEventInfo.ReflectedType;
157 Attribute[][] array = new Attribute[num][];
158 while (type != typeof(object))
159 {
160 MemberInfo @event = type.GetEvent(name, bindingAttr);
161 if (@event != null)
162 {
164 }
165 type = type.BaseType;
166 }
167 Attribute[][] array2 = array;
168 foreach (Attribute[] array3 in array2)
169 {
170 if (array3 != null)
171 {
172 Attribute[] array4 = array3;
173 foreach (Attribute value in array4)
174 {
175 attributes.Add(value);
176 }
177 }
178 }
179 }
180
181 private void FillMethods()
182 {
183 if (_filledMethods)
184 {
185 return;
186 }
187 if (_realEvent != null)
188 {
191 EventInfo eventInfo = null;
192 if (_addMethod == null || _removeMethod == null)
193 {
194 Type baseType = _componentClass.BaseType;
195 while (baseType != null && baseType != typeof(object))
196 {
197 BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
198 EventInfo @event = baseType.GetEvent(_realEvent.Name, bindingAttr);
199 if (@event.GetAddMethod() != null)
200 {
201 eventInfo = @event;
202 break;
203 }
204 }
205 }
206 if (eventInfo != null)
207 {
208 _addMethod = eventInfo.GetAddMethod();
209 _removeMethod = eventInfo.GetRemoveMethod();
210 _type = eventInfo.EventHandlerType;
211 }
212 else
213 {
215 }
216 }
217 else
218 {
220 if (_realEvent != null)
221 {
222 FillMethods();
223 return;
224 }
225 Type[] args = new Type[1] { _type };
226 _addMethod = MemberDescriptor.FindMethod(_componentClass, "AddOn" + Name, args, typeof(void));
227 _removeMethod = MemberDescriptor.FindMethod(_componentClass, "RemoveOn" + Name, args, typeof(void));
228 if (_addMethod == null || _removeMethod == null)
229 {
231 }
232 }
233 _filledMethods = true;
234 }
235
236 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "currentReflectType is in _componentClass's hierarchy. Since _componentClass is annotated with All, this means currentReflectType is annotated with All as well.")]
237 private void FillSingleMethodAttribute(MethodInfo realMethodInfo, IList attributes)
238 {
239 string name = realMethodInfo.Name;
240 BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public;
241 Type type = realMethodInfo.ReflectedType;
242 int num = 0;
243 while (type != null && type != typeof(object))
244 {
245 num++;
246 type = type.BaseType;
247 }
248 if (num <= 0)
249 {
250 return;
251 }
252 type = realMethodInfo.ReflectedType;
253 Attribute[][] array = new Attribute[num][];
254 while (type != null && type != typeof(object))
255 {
256 MemberInfo method = type.GetMethod(name, bindingAttr);
257 if (method != null)
258 {
260 }
261 type = type.BaseType;
262 }
263 Attribute[][] array2 = array;
264 foreach (Attribute[] array3 in array2)
265 {
266 if (array3 != null)
267 {
268 Attribute[] array4 = array3;
269 foreach (Attribute value in array4)
270 {
271 attributes.Add(value);
272 }
273 }
274 }
275 }
276
277 public override void RemoveEventHandler(object component, Delegate value)
278 {
279 FillMethods();
280 if (component == null)
281 {
282 return;
283 }
284 ISite site = MemberDescriptor.GetSite(component);
285 IComponentChangeService componentChangeService = null;
286 if (site != null)
287 {
288 componentChangeService = (IComponentChangeService)site.GetService(typeof(IComponentChangeService));
289 }
290 if (componentChangeService != null)
291 {
292 try
293 {
294 componentChangeService.OnComponentChanging(component, this);
295 }
296 catch (CheckoutException ex)
297 {
298 if (ex == CheckoutException.Canceled)
299 {
300 return;
301 }
302 throw;
303 }
304 componentChangeService.OnComponentChanging(component, this);
305 }
306 bool flag = false;
307 if (site != null && site.DesignMode)
308 {
309 IDictionaryService dictionaryService = (IDictionaryService)site.GetService(typeof(IDictionaryService));
310 if (dictionaryService != null)
311 {
312 Delegate source = (Delegate)dictionaryService.GetValue(this);
314 dictionaryService.SetValue(this, source);
315 flag = true;
316 }
317 }
318 if (!flag)
319 {
320 MethodInfo removeMethod = _removeMethod;
321 object[] parameters = new Delegate[1] { value };
322 removeMethod.Invoke(component, parameters);
323 }
324 componentChangeService?.OnComponentChanged(component, this, null, value);
325 }
326}
static readonly CheckoutException Canceled
static ? ISite GetSite(object? component)
static ? MethodInfo FindMethod([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type componentClass, string name, Type[] args, Type returnType)
override void RemoveEventHandler(object component, Delegate value)
void FillSingleMethodAttribute(MethodInfo realMethodInfo, IList attributes)
ReflectEventDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentClass, EventInfo eventInfo)
ReflectEventDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentClass, string name, Type type, Attribute[] attributes)
void FillEventInfoAttribute(EventInfo realEventInfo, IList attributes)
ReflectEventDescriptor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType, EventDescriptor oldReflectEventDescriptor, Attribute[] attributes)
override void AddEventHandler(object component, Delegate value)
static ? Delegate Remove(Delegate? source, Delegate? value)
Definition Delegate.cs:463
static ? Delegate Combine(Delegate? a, Delegate? b)
Definition Delegate.cs:379
MethodInfo? GetAddMethod()
Definition EventInfo.cs:59
virtual ? Type EventHandlerType
Definition EventInfo.cs:31
MethodInfo? GetRemoveMethod()
Definition EventInfo.cs:64
object? Invoke(object? obj, object?[]? parameters)
static string InvalidNullArgument
Definition SR.cs:86
static string ErrorInvalidEventType
Definition SR.cs:28
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ErrorInvalidEventHandler
Definition SR.cs:26
static string ErrorMissingEventAccessors
Definition SR.cs:32
Definition SR.cs:7
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
EventInfo? GetEvent(string name)
Definition Type.cs:589
Type? BaseType
Definition Type.cs:295
void OnComponentChanged(object component, MemberDescriptor? member, object? oldValue, object? newValue)
void OnComponentChanging(object component, MemberDescriptor? member)
void SetValue(object key, object? value)
object? GetService(Type serviceType)