System.Reflection.CustomAttribute.GetPropertyOrFieldData C# (CSharp) Method

GetPropertyOrFieldData() private static method

private static GetPropertyOrFieldData ( Module module, IntPtr &blobStart, IntPtr blobEnd, string &name, bool &isProperty, Type &type, object &value ) : void
module Module
blobStart System.IntPtr
blobEnd System.IntPtr
name string
isProperty bool
type System.Type
value object
return void
        private unsafe static void GetPropertyOrFieldData(
            Module module, ref IntPtr blobStart, IntPtr blobEnd, out string name, out bool isProperty, out Type type, out object value)
        {
            byte* pBlobStart = (byte*)blobStart;
            _GetPropertyOrFieldData(
                (IntPtr)module.ModuleHandle.Value, &pBlobStart, (byte*)blobEnd, out name, out isProperty, out type, out value);
            blobStart = (IntPtr)pBlobStart;
        }
        #endregion

Usage Example

Exemplo n.º 1
0
        private static unsafe object[] GetCustomAttributes(RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes, bool isDecoratedTargetSecurityTransparent)
        {
            if (decoratedModule.Assembly.ReflectionOnly)
            {
                throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA"));
            }
            MetadataImport metadataImport = decoratedModule.MetadataImport;

            CustomAttributeRecord[] attributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
            Type elementType = (attributeFilterType == (RuntimeType)null || attributeFilterType.IsValueType ? 1 : (attributeFilterType.ContainsGenericParameters ? 1 : 0)) != 0 ? typeof(object) : (Type)attributeFilterType;

            if (attributeFilterType == (RuntimeType)null && attributeRecords.Length == 0)
            {
                return(CustomAttribute.CreateAttributeArrayHelper(elementType, 0));
            }
            object[]             attributeArrayHelper1 = CustomAttribute.CreateAttributeArrayHelper(elementType, attributeRecords.Length);
            int                  length = 0;
            SecurityContextFrame securityContextFrame = new SecurityContextFrame();

            securityContextFrame.Push(decoratedModule.GetRuntimeAssembly());
            Assembly lastAptcaOkAssembly = (Assembly)null;

            for (int index1 = 0; index1 < attributeRecords.Length; ++index1)
            {
                CustomAttributeRecord caRecord      = attributeRecords[index1];
                IRuntimeMethodInfo    ctor          = (IRuntimeMethodInfo)null;
                RuntimeType           attributeType = (RuntimeType)null;
                int    namedArgs = 0;
                IntPtr ptr1      = caRecord.blob.Signature;
                IntPtr blobEnd   = (IntPtr)((void *)((IntPtr)(void *)ptr1 + caRecord.blob.Length));
                int    num       = (int)((sbyte *)(void *)blobEnd - (sbyte *)(void *)ptr1);
                bool   ctorHasParameters;
                bool   isVarArg;
                if (CustomAttribute.FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, (MetadataToken)decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributeArrayHelper1, derivedAttributes, out attributeType, out ctor, out ctorHasParameters, out isVarArg))
                {
                    if (ctor != null)
                    {
                        RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent);
                    }
                    RuntimeConstructorInfo.CheckCanCreateInstance((Type)attributeType, isVarArg);
                    object target;
                    if (ctorHasParameters)
                    {
                        target = CustomAttribute.CreateCaObject(decoratedModule, ctor, ref ptr1, blobEnd, out namedArgs);
                    }
                    else
                    {
                        target = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor);
                        if (num == 0)
                        {
                            namedArgs = 0;
                        }
                        else
                        {
                            if ((int)Marshal.ReadInt16(ptr1) != 1)
                            {
                                throw new CustomAttributeFormatException();
                            }
                            IntPtr ptr2 = (IntPtr)((void *)((IntPtr)(void *)ptr1 + 2));
                            namedArgs = (int)Marshal.ReadInt16(ptr2);
                            ptr1      = (IntPtr)((void *)((IntPtr)(void *)ptr2 + 2));
                        }
                    }
                    for (int index2 = 0; index2 < namedArgs; ++index2)
                    {
                        IntPtr      signature = caRecord.blob.Signature;
                        string      name;
                        bool        isProperty;
                        RuntimeType type;
                        object      obj;
                        CustomAttribute.GetPropertyOrFieldData(decoratedModule, ref ptr1, blobEnd, out name, out isProperty, out type, out obj);
                        try
                        {
                            if (isProperty)
                            {
                                if (type == (RuntimeType)null && obj != null)
                                {
                                    type = (RuntimeType)obj.GetType();
                                    if (type == CustomAttribute.Type_RuntimeType)
                                    {
                                        type = CustomAttribute.Type_Type;
                                    }
                                }
                                RuntimePropertyInfo runtimePropertyInfo = !(type == (RuntimeType)null) ? attributeType.GetProperty(name, (Type)type, Type.EmptyTypes) as RuntimePropertyInfo : attributeType.GetProperty(name) as RuntimePropertyInfo;
                                if ((PropertyInfo)runtimePropertyInfo == (PropertyInfo)null)
                                {
                                    throw new CustomAttributeFormatException(string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Environment.GetResourceString(isProperty ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), (object)name));
                                }
                                RuntimeMethodInfo runtimeMethodInfo = runtimePropertyInfo.GetSetMethod(true) as RuntimeMethodInfo;
                                if (runtimeMethodInfo.IsPublic)
                                {
                                    RuntimeMethodHandle.CheckLinktimeDemands((IRuntimeMethodInfo)runtimeMethodInfo, decoratedModule, isDecoratedTargetSecurityTransparent);
                                    runtimeMethodInfo.UnsafeInvoke(target, BindingFlags.Default, (Binder)null, new object[1] {
                                        obj
                                    }, (CultureInfo)null);
                                }
                            }
                            else
                            {
                                RtFieldInfo rtFieldInfo = attributeType.GetField(name) as RtFieldInfo;
                                if (isDecoratedTargetSecurityTransparent)
                                {
                                    RuntimeFieldHandle.CheckAttributeAccess(rtFieldInfo.FieldHandle, decoratedModule.GetNativeHandle());
                                }
                                rtFieldInfo.CheckConsistency(target);
                                rtFieldInfo.UnsafeSetValue(target, obj, BindingFlags.Default, Type.DefaultBinder, (CultureInfo)null);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new CustomAttributeFormatException(string.Format((IFormatProvider)CultureInfo.CurrentUICulture, Environment.GetResourceString(isProperty ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), (object)name), ex);
                        }
                    }
                    if (!ptr1.Equals((object)blobEnd))
                    {
                        throw new CustomAttributeFormatException();
                    }
                    attributeArrayHelper1[length++] = target;
                }
            }
            securityContextFrame.Pop();
            if (length == attributeRecords.Length && pcaCount == 0)
            {
                return(attributeArrayHelper1);
            }
            object[] attributeArrayHelper2 = CustomAttribute.CreateAttributeArrayHelper(elementType, length + pcaCount);
            Array.Copy((Array)attributeArrayHelper1, 0, (Array)attributeArrayHelper2, 0, length);
            return(attributeArrayHelper2);
        }
All Usage Examples Of System.Reflection.CustomAttribute::GetPropertyOrFieldData