Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NullabilityInfoContext.cs
Go to the documentation of this file.
4
5namespace System.Reflection;
6
7public sealed class NullabilityInfoContext
8{
9 [Flags]
10 private enum NotAnnotatedStatus
11 {
12 None = 0,
13 Private = 1,
14 Internal = 2
15 }
16
18
20
21 internal static bool IsSupported { get; } = !AppContext.TryGetSwitch("System.Reflection.NullabilityInfoContext.IsSupported", out var isEnabled) || isEnabled;
22
23
25 {
26 while (memberInfo != null)
27 {
29 {
30 return value;
31 }
32 foreach (CustomAttributeData customAttributesDatum in memberInfo.GetCustomAttributesData())
33 {
34 if (customAttributesDatum.AttributeType.Name == "NullableContextAttribute" && customAttributesDatum.AttributeType.Namespace == "System.Runtime.CompilerServices" && customAttributesDatum.ConstructorArguments.Count == 1)
35 {
36 value = TranslateByte(customAttributesDatum.ConstructorArguments[0].Value);
38 return value;
39 }
40 }
41 memberInfo = memberInfo.DeclaringType;
42 }
43 return NullabilityState.Unknown;
44 }
45
66
68 {
69 if (!(parameter.Member is MethodInfo method))
70 {
71 return;
72 }
75 if (string.IsNullOrEmpty(parameter.Name))
76 {
78 }
79 else
80 {
81 ParameterInfo[] parameters = methodMetadataDefinition.GetParameters();
82 for (int i = 0; i < parameters.Length; i++)
83 {
84 if (parameter.Position == i && parameter.Name == parameters[i].Name)
85 {
86 parameterInfo = parameters[i];
87 break;
88 }
89 }
90 }
91 if (parameterInfo != null)
92 {
94 }
95 }
96
98 {
99 if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
100 {
101 method = method.GetGenericMethodDefinition();
102 }
104 }
105
107 {
108 foreach (CustomAttributeData attribute in attributes)
109 {
110 if (attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis")
111 {
112 if (attribute.AttributeType.Name == "NotNullAttribute" && nullability.ReadState == NullabilityState.Nullable)
113 {
114 nullability.ReadState = NullabilityState.NotNull;
115 break;
116 }
117 if ((attribute.AttributeType.Name == "MaybeNullAttribute" || attribute.AttributeType.Name == "MaybeNullWhenAttribute") && nullability.ReadState == NullabilityState.NotNull && !nullability.Type.IsValueType)
118 {
119 nullability.ReadState = NullabilityState.Nullable;
120 break;
121 }
122 if (attribute.AttributeType.Name == "DisallowNullAttribute" && nullability.WriteState == NullabilityState.Nullable)
123 {
124 nullability.WriteState = NullabilityState.NotNull;
125 break;
126 }
127 if (attribute.AttributeType.Name == "AllowNullAttribute" && nullability.WriteState == NullabilityState.NotNull && !nullability.Type.IsValueType)
128 {
129 nullability.WriteState = NullabilityState.Nullable;
130 break;
131 }
132 }
133 }
134 }
135
137 {
138 if ((object)propertyInfo == null)
139 {
140 throw new ArgumentNullException("propertyInfo");
141 }
143 NullabilityInfo nullabilityInfo = GetNullabilityInfo(propertyInfo, propertyInfo.PropertyType, propertyInfo.GetCustomAttributesData());
144 MethodInfo getMethod = propertyInfo.GetGetMethod(nonPublic: true);
145 MethodInfo setMethod = propertyInfo.GetSetMethod(nonPublic: true);
146 if (getMethod != null)
147 {
149 {
150 nullabilityInfo.ReadState = NullabilityState.Unknown;
151 }
152 CheckNullabilityAttributes(nullabilityInfo, getMethod.ReturnParameter.GetCustomAttributesData());
153 }
154 else
155 {
156 nullabilityInfo.ReadState = NullabilityState.Unknown;
157 }
158 if (setMethod != null)
159 {
161 {
162 nullabilityInfo.WriteState = NullabilityState.Unknown;
163 }
164 CheckNullabilityAttributes(nullabilityInfo, setMethod.GetParameters()[0].GetCustomAttributesData());
165 }
166 else
167 {
168 nullabilityInfo.WriteState = NullabilityState.Unknown;
169 }
170 return nullabilityInfo;
171 }
172
174 {
175 if ((method.IsPrivate || method.IsFamilyAndAssembly || method.IsAssembly) && IsPublicOnly(method.IsPrivate, method.IsFamilyAndAssembly, method.IsAssembly, method.Module))
176 {
177 return true;
178 }
179 return false;
180 }
181
183 {
184 if ((object)eventInfo == null)
185 {
186 throw new ArgumentNullException("eventInfo");
187 }
189 return GetNullabilityInfo(eventInfo, eventInfo.EventHandlerType, eventInfo.GetCustomAttributesData());
190 }
191
208
209 private static void EnsureIsSupported()
210 {
211 if (!IsSupported)
212 {
214 }
215 }
216
218 {
219 if ((fieldInfo.IsPrivate || fieldInfo.IsFamilyAndAssembly || fieldInfo.IsAssembly) && IsPublicOnly(fieldInfo.IsPrivate, fieldInfo.IsFamilyAndAssembly, fieldInfo.IsAssembly, fieldInfo.Module))
220 {
221 return true;
222 }
223 return false;
224 }
225
227 {
229 {
230 value = PopulateAnnotationInfo(module.GetCustomAttributesData());
232 }
233 if (value == NotAnnotatedStatus.None)
234 {
235 return false;
236 }
237 if (((isPrivate || isFamilyAndAssembly) && value.HasFlag(NotAnnotatedStatus.Private)) || (isAssembly && value.HasFlag(NotAnnotatedStatus.Internal)))
238 {
239 return true;
240 }
241 return false;
242 }
243
245 {
246 System.Runtime.CompilerServices.Unsafe.SkipInit(out bool flag);
248 {
249 if (customAttribute.AttributeType.Name == "NullablePublicOnlyAttribute" && customAttribute.AttributeType.Namespace == "System.Runtime.CompilerServices" && customAttribute.ConstructorArguments.Count == 1)
250 {
251 object value = customAttribute.ConstructorArguments[0].Value;
252 int num;
253 if (value is bool)
254 {
255 flag = (bool)value;
256 num = 1;
257 }
258 else
259 {
260 num = 0;
261 }
262 if (((uint)num & (flag ? 1u : 0u)) != 0)
263 {
264 return NotAnnotatedStatus.Private | NotAnnotatedStatus.Internal;
265 }
266 return NotAnnotatedStatus.Private;
267 }
268 }
269 return NotAnnotatedStatus.None;
270 }
271
276
278 {
282 Type type2 = type;
283 if (type.IsValueType)
284 {
286 if (type2 != null)
287 {
288 state = NullabilityState.Nullable;
289 }
290 else
291 {
292 type2 = type;
293 state = NullabilityState.NotNull;
294 }
295 }
296 else
297 {
299 {
301 }
302 if (type.IsArray)
303 {
305 }
306 }
307 if (type2.IsGenericType)
308 {
309 Type[] genericArguments = type2.GetGenericArguments();
311 int i = 0;
312 int num = 0;
313 for (; i < genericArguments.Length; i++)
314 {
316 if (!type3.IsValueType || type3.IsGenericType)
317 {
318 num++;
319 }
321 }
322 }
324 if (!type.IsValueType && state != 0)
325 {
327 }
328 return nullabilityInfo;
329 }
330
332 {
334 {
335 if (customAttribute.AttributeType.Name == "NullableAttribute" && customAttribute.AttributeType.Namespace == "System.Runtime.CompilerServices" && customAttribute.ConstructorArguments.Count == 1)
336 {
337 object value = customAttribute.ConstructorArguments[0].Value;
338 if (value is byte b)
339 {
341 return true;
342 }
344 {
346 return true;
347 }
348 break;
349 }
350 }
351 return false;
352 }
353
371
373 {
375 if (declaringType != null && declaringType.IsGenericType && !declaringType.IsGenericTypeDefinition)
376 {
377 return declaringType.GetGenericTypeDefinition().GetMemberWithSameMetadataDefinitionAs(member);
378 }
379 return member;
380 }
381
383 {
384 MethodInfo getMethod = property.GetGetMethod(nonPublic: true);
385 if ((object)getMethod != null)
386 {
387 return getMethod.ReturnType;
388 }
389 return property.GetSetMethod(nonPublic: true).GetParameters()[0].ParameterType;
390 }
391
393 {
394 if (metaType.IsGenericParameter)
395 {
397 if (state == NullabilityState.NotNull && !ParseNullableState(metaType.GetCustomAttributesData(), 0, ref state))
398 {
400 }
401 nullability.ReadState = state;
402 nullability.WriteState = state;
403 }
404 else
405 {
406 if (!metaType.ContainsGenericParameters)
407 {
408 return;
409 }
410 if (nullability.GenericTypeArguments.Length != 0)
411 {
412 Type[] genericArguments = metaType.GetGenericArguments();
413 for (int i = 0; i < genericArguments.Length; i++)
414 {
415 if (genericArguments[i].IsGenericParameter)
416 {
418 nullability.GenericTypeArguments[i].ReadState = nullabilityInfo.ReadState;
419 nullability.GenericTypeArguments[i].WriteState = nullabilityInfo.WriteState;
420 }
421 else
422 {
423 UpdateGenericArrayElements(nullability.GenericTypeArguments[i].ElementType, metaMember, genericArguments[i]);
424 }
425 }
426 }
427 else
428 {
430 }
431 }
432 }
433
435 {
436 if (metaType.IsArray && elementState != null && metaType.GetElementType().IsGenericParameter)
437 {
438 Type elementType = metaType.GetElementType();
440 elementState.ReadState = nullabilityInfo.ReadState;
441 elementState.WriteState = nullabilityInfo.WriteState;
442 }
443 }
444
445 private static NullabilityState TranslateByte(object value)
446 {
447 if (!(value is byte b))
448 {
449 return NullabilityState.Unknown;
450 }
451 return TranslateByte(b);
452 }
453
454 private static NullabilityState TranslateByte(byte b)
455 {
456 return b switch
457 {
458 1 => NullabilityState.NotNull,
459 2 => NullabilityState.Nullable,
460 _ => NullabilityState.Unknown,
461 };
462 }
463}
static bool TryGetSwitch(string switchName, out bool isEnabled)
Definition AppContext.cs:74
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
void Add(TKey key, TValue value)
virtual IList< CustomAttributeData > GetCustomAttributesData()
Definition MemberInfo.cs:77
NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, IList< CustomAttributeData > customAttributes)
NotAnnotatedStatus PopulateAnnotationInfo(IList< CustomAttributeData > customAttributes)
NullabilityInfo Create(ParameterInfo parameterInfo)
NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, IList< CustomAttributeData > customAttributes, int index)
static bool ParseNullableState(IList< CustomAttributeData > customAttributes, int index, ref NullabilityState state)
NullabilityInfo Create(PropertyInfo propertyInfo)
static NullabilityState TranslateByte(object value)
void CheckParameterMetadataType(ParameterInfo parameter, NullabilityInfo nullability)
bool IsPrivateOrInternalFieldAndAnnotationDisabled(FieldInfo fieldInfo)
bool IsPublicOnly(bool isPrivate, bool isFamilyAndAssembly, bool isAssembly, Module module)
bool IsPrivateOrInternalMethodAndAnnotationDisabled(MethodInfo method)
NullabilityInfo Create(FieldInfo fieldInfo)
NullabilityInfo Create(EventInfo eventInfo)
void CheckGenericParameters(NullabilityInfo nullability, MemberInfo metaMember, Type metaType)
static NullabilityState TranslateByte(byte b)
readonly Dictionary< MemberInfo, NullabilityState > _context
readonly Dictionary< Module, NotAnnotatedStatus > _publicOnlyModules
void TryLoadGenericMetaTypeNullability(MemberInfo memberInfo, NullabilityInfo nullability)
static Type GetPropertyMetaType(PropertyInfo property)
static MethodInfo GetMethodMetadataDefinition(MethodInfo method)
NullabilityState GetNullableContext(MemberInfo memberInfo)
static MemberInfo GetMemberMetadataDefinition(MemberInfo member)
void UpdateGenericArrayElements(NullabilityInfo elementState, MemberInfo metaMember, Type metaType)
void CheckNullabilityAttributes(NullabilityInfo nullability, IList< CustomAttributeData > attributes)
static string NullabilityInfoContext_NotSupported
Definition SR.cs:2212
Definition SR.cs:7
static ? Type GetUnderlyingType(Type nullableType)
Definition Nullable.cs:139