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

CreateCaObject() private static method

private static CreateCaObject ( Module module, RuntimeMethodHandle ctor, IntPtr &blob, IntPtr blobEnd, int &namedArgs ) : Object
module Module
ctor System.RuntimeMethodHandle
blob System.IntPtr
blobEnd System.IntPtr
namedArgs int
return Object
        private static unsafe Object CreateCaObject(Module module, RuntimeMethodHandle ctor, ref IntPtr blob, IntPtr blobEnd, out int namedArgs)
        {
            byte* pBlob = (byte*)blob;
            byte* pBlobEnd = (byte*)blobEnd;
            int cNamedArgs; 
            object ca = _CreateCaObject((void*)module.ModuleHandle.Value, (void*)ctor.Value, &pBlob, pBlobEnd, &cNamedArgs);
            blob = (IntPtr)pBlob;
            namedArgs = cNamedArgs;
            return ca;
        }

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::CreateCaObject