Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ AddCustomAttributes()

static unsafe void System.Reflection.CustomAttribute.AddCustomAttributes ( ref RuntimeType::ListBuilder< object > attributes,
RuntimeModule decoratedModule,
int decoratedMetadataToken,
RuntimeType attributeFilterType,
bool mustBeInheritable,
RuntimeType::ListBuilder< object > derivedAttributes )
inlinestaticprivate

Definition at line 300 of file CustomAttribute.cs.

301 {
302 CustomAttributeRecord[] customAttributeRecords = RuntimeCustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
303 if ((object)attributeFilterType == null && customAttributeRecords.Length == 0)
304 {
305 return;
306 }
307 MetadataImport scope = decoratedModule.MetadataImport;
308 for (int i = 0; i < customAttributeRecords.Length; i++)
309 {
310 ref CustomAttributeRecord reference = ref customAttributeRecords[i];
311 IntPtr blob = reference.blob.Signature;
312 IntPtr intPtr = (IntPtr)((byte*)(void*)blob + reference.blob.Length);
313 if (!FilterCustomAttributeRecord(reference.tkCtor, in scope, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, ref derivedAttributes, out var attributeType, out var ctorWithParameters, out var isVarArg))
314 {
315 continue;
316 }
317 RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, isVarArg);
318 object obj;
319 int namedArgs;
320 if (ctorWithParameters != null)
321 {
322 obj = CreateCaObject(decoratedModule, attributeType, ctorWithParameters, ref blob, intPtr, out namedArgs);
323 }
324 else
325 {
326 obj = attributeType.CreateInstanceDefaultCtor(publicOnly: false, wrapExceptions: false);
327 if ((int)((byte*)(void*)intPtr - (byte*)(void*)blob) == 0)
328 {
329 namedArgs = 0;
330 }
331 else
332 {
333 int num = Unsafe.ReadUnaligned<int>((void*)blob);
334 if ((num & 0xFFFF) != 1)
335 {
336 throw new CustomAttributeFormatException();
337 }
338 namedArgs = num >> 16;
339 blob = (IntPtr)((byte*)(void*)blob + 4);
340 }
341 }
342 for (int j = 0; j < namedArgs; j++)
343 {
344 GetPropertyOrFieldData(decoratedModule, ref blob, intPtr, out var name, out var isProperty, out var type, out var value);
345 try
346 {
347 if (isProperty)
348 {
349 if ((object)type == null && value != null)
350 {
351 type = (RuntimeType)value.GetType();
352 if (type == Type_RuntimeType)
353 {
354 type = Type_Type;
355 }
356 }
357 PropertyInfo propertyInfo = (((object)type == null) ? attributeType.GetProperty(name) : attributeType.GetProperty(name, type, Type.EmptyTypes));
358 if ((object)propertyInfo == null)
359 {
360 throw new CustomAttributeFormatException(SR.Format(SR.RFLCT_InvalidPropFail, name));
361 }
362 MethodInfo setMethod = propertyInfo.GetSetMethod(nonPublic: true);
363 if (setMethod.IsPublic)
364 {
365 setMethod.Invoke(obj, BindingFlags.Default, null, new object[1] { value }, null);
366 }
367 }
368 else
369 {
370 FieldInfo field = attributeType.GetField(name);
371 field.SetValue(obj, value, BindingFlags.Default, Type.DefaultBinder, null);
372 }
373 }
374 catch (Exception inner)
375 {
376 throw new CustomAttributeFormatException(SR.Format(isProperty ? SR.RFLCT_InvalidPropFail : SR.RFLCT_InvalidFieldFail, name), inner);
377 }
378 }
379 if (blob != intPtr)
380 {
381 throw new CustomAttributeFormatException();
382 }
383 attributes.Add(obj);
384 }
385 }
static unsafe object CreateCaObject(RuntimeModule module, RuntimeType type, IRuntimeMethodInfo ctor, ref IntPtr blob, IntPtr blobEnd, out int namedArgs)
static bool FilterCustomAttributeRecord(MetadataToken caCtorToken, in MetadataImport scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, ref RuntimeType.ListBuilder< object > derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctorWithParameters, out bool isVarArg)
static readonly RuntimeType Type_RuntimeType
static unsafe void GetPropertyOrFieldData(RuntimeModule module, ref IntPtr blobStart, IntPtr blobEnd, out string name, out bool isProperty, out RuntimeType type, out object value)
static readonly RuntimeType Type_Type

References System.Reflection.RuntimeConstructorInfo.CheckCanCreateInstance(), System.Reflection.CustomAttribute.CreateCaObject(), System.Type.DefaultBinder, System.Type.EmptyTypes, System.Reflection.CustomAttribute.FilterCustomAttributeRecord(), System.SR.Format(), System.Reflection.RuntimeCustomAttributeData.GetCustomAttributeRecords(), System.Reflection.CustomAttribute.GetPropertyOrFieldData(), System.Reflection.PropertyInfo.GetSetMethod(), System.Reflection.MethodBase.Invoke(), System.Reflection.MethodBase.IsPublic, System.Reflection.RuntimeModule.MetadataImport, System.obj, System.SR.RFLCT_InvalidFieldFail, System.SR.RFLCT_InvalidPropFail, System.Reflection.FieldInfo.SetValue(), System.type, System.Reflection.CustomAttribute.Type_RuntimeType, System.Reflection.CustomAttribute.Type_Type, and System.value.

Referenced by System.Reflection.CustomAttribute.GetCustomAttributes(), System.Reflection.CustomAttribute.GetCustomAttributes(), and System.Reflection.CustomAttribute.GetCustomAttributes().