当前位置: 首页>>代码示例>>C#>>正文


C# RuntimeModule.GetRuntimeAssembly方法代码示例

本文整理汇总了C#中System.Reflection.RuntimeModule.GetRuntimeAssembly方法的典型用法代码示例。如果您正苦于以下问题:C# RuntimeModule.GetRuntimeAssembly方法的具体用法?C# RuntimeModule.GetRuntimeAssembly怎么用?C# RuntimeModule.GetRuntimeAssembly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.RuntimeModule的用法示例。


在下文中一共展示了RuntimeModule.GetRuntimeAssembly方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetCustomAttributes

        private unsafe static 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"));
            Contract.EndContractBlock();

            MetadataImport scope = decoratedModule.MetadataImport;
            CustomAttributeRecord[] car = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);

            bool useObjectArray = (attributeFilterType == null || attributeFilterType.IsValueType || attributeFilterType.ContainsGenericParameters);
            Type arrayType = useObjectArray ? typeof(object) : attributeFilterType;

            if (attributeFilterType == null && car.Length == 0)
                return CreateAttributeArrayHelper(arrayType, 0);

            object[] attributes = CreateAttributeArrayHelper(arrayType, car.Length);
            int cAttributes = 0;

            // Custom attribute security checks are done with respect to the assembly *decorated* with the 
            // custom attribute as opposed to the *caller of GetCustomAttributes*.
            // Since this assembly might not be on the stack and the attribute ctor or property setters we're about to invoke may
            // make security demands, we push a frame on the stack as a proxy for the decorated assembly (this frame will be picked
            // up an interpreted by the security stackwalker).
            // Once we push the frame it will be automatically popped in the event of an exception, so no need to use CERs or the
            // like.
            SecurityContextFrame frame = new SecurityContextFrame();
            frame.Push(decoratedModule.GetRuntimeAssembly());

            // Optimization for the case where attributes decorate entities in the same assembly in which case 
            // we can cache the successful APTCA check between the decorated and the declared assembly.
            Assembly lastAptcaOkAssembly = null;

            for (int i = 0; i < car.Length; i++)
            {
                object attribute = null;
                CustomAttributeRecord caRecord = car[i];

                IRuntimeMethodInfo ctor = null;
                RuntimeType attributeType = null;
                bool ctorHasParameters, isVarArg;
                int cNamedArgs = 0;

                IntPtr blobStart = caRecord.blob.Signature;
                IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length);
                int blobLen = (int)((byte*)blobEnd - (byte*)blobStart);

                if (!FilterCustomAttributeRecord(caRecord, scope, ref lastAptcaOkAssembly, 
                                                 decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, 
                                                 attributes, derivedAttributes,
                                                 out attributeType, out ctor, out ctorHasParameters, out isVarArg))
                    continue;

                if (ctor != null)
                {
                    // Linktime demand checks 
                    // decoratedMetadataToken needed as it may be "transparent" in which case we do a full stack walk
                    RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent);
                }
                else
                {

                }

                // Leverage RuntimeConstructorInfo standard .ctor verfication
                RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, isVarArg); 

                // Create custom attribute object
                if (ctorHasParameters)
                {
                    attribute = CreateCaObject(decoratedModule, ctor, ref blobStart, blobEnd, out cNamedArgs); 
                }
                else
                {
                    attribute = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor);

                    // It is allowed by the ECMA spec to have an empty signature blob
                    if (blobLen == 0)
                        cNamedArgs = 0;
                    else
                    {
                        // Metadata is always written in little-endian format. Must account for this on
                        // big-endian platforms.
#if BIGENDIAN
                        const int CustomAttributeVersion = 0x0100;
#else
                        const int CustomAttributeVersion = 0x0001;
#endif
                        if (Marshal.ReadInt16(blobStart) != CustomAttributeVersion)
                            throw new CustomAttributeFormatException();
                        blobStart = (IntPtr)((byte*)blobStart + 2); // skip version prefix

                        cNamedArgs = Marshal.ReadInt16(blobStart);
                        blobStart = (IntPtr)((byte*)blobStart + 2); // skip namedArgs count
#if BIGENDIAN
                        cNamedArgs = ((cNamedArgs & 0xff00) >> 8) | ((cNamedArgs & 0x00ff) << 8);
#endif
                    }
                }
//.........这里部分代码省略.........
开发者ID:enavro,项目名称:coreclr,代码行数:101,代码来源:CustomAttribute.cs

示例2: GetCustomAttributes

 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[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
     Type elementType = (((attributeFilterType == null) || attributeFilterType.IsValueType) || attributeFilterType.ContainsGenericParameters) ? typeof(object) : attributeFilterType;
     if ((attributeFilterType == null) && (customAttributeRecords.Length == 0))
     {
         return CreateAttributeArrayHelper(elementType, 0);
     }
     object[] attributes = CreateAttributeArrayHelper(elementType, customAttributeRecords.Length);
     int length = 0;
     SecurityContextFrame frame = new SecurityContextFrame();
     frame.Push(decoratedModule.GetRuntimeAssembly());
     Assembly lastAptcaOkAssembly = null;
     for (int i = 0; i < customAttributeRecords.Length; i++)
     {
         bool flag2;
         bool flag3;
         object obj2 = null;
         CustomAttributeRecord caRecord = customAttributeRecords[i];
         IRuntimeMethodInfo ctor = null;
         RuntimeType attributeType = null;
         int namedArgs = 0;
         IntPtr signature = caRecord.blob.Signature;
         IntPtr blobEnd = (IntPtr) (((void*) signature) + caRecord.blob.Length);
         int num4 = (int) ((long) ((((void*) blobEnd) - ((void*) signature)) / 1));
         if (FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributes, derivedAttributes, out attributeType, out ctor, out flag2, out flag3))
         {
             if (ctor != null)
             {
                 RuntimeMethodHandle.CheckLinktimeDemands(ctor, decoratedModule, isDecoratedTargetSecurityTransparent);
             }
             RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, flag3);
             if (flag2)
             {
                 obj2 = CreateCaObject(decoratedModule, ctor, ref signature, blobEnd, out namedArgs);
             }
             else
             {
                 obj2 = RuntimeTypeHandle.CreateCaInstance(attributeType, ctor);
                 if (num4 == 0)
                 {
                     namedArgs = 0;
                 }
                 else
                 {
                     if (Marshal.ReadInt16(signature) != 1)
                     {
                         throw new CustomAttributeFormatException();
                     }
                     signature = (IntPtr) (((void*) signature) + 2);
                     namedArgs = Marshal.ReadInt16(signature);
                     signature = (IntPtr) (((void*) signature) + 2);
                 }
             }
             for (int j = 0; j < namedArgs; j++)
             {
                 string str;
                 bool flag4;
                 RuntimeType type;
                 object obj3;
                 IntPtr ptr1 = caRecord.blob.Signature;
                 GetPropertyOrFieldData(decoratedModule, ref signature, blobEnd, out str, out flag4, out type, out obj3);
                 try
                 {
                     if (flag4)
                     {
                         if ((type == null) && (obj3 != null))
                         {
                             type = (RuntimeType) obj3.GetType();
                             if (type == Type_RuntimeType)
                             {
                                 type = Type_Type;
                             }
                         }
                         RuntimePropertyInfo property = null;
                         if (type == null)
                         {
                             property = attributeType.GetProperty(str) as RuntimePropertyInfo;
                         }
                         else
                         {
                             property = attributeType.GetProperty(str, type, Type.EmptyTypes) as RuntimePropertyInfo;
                         }
                         if (property == null)
                         {
                             throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }));
                         }
                         RuntimeMethodInfo setMethod = property.GetSetMethod(true) as RuntimeMethodInfo;
                         if (setMethod.IsPublic)
                         {
                             RuntimeMethodHandle.CheckLinktimeDemands(setMethod, decoratedModule, isDecoratedTargetSecurityTransparent);
                             setMethod.Invoke(obj2, BindingFlags.Default, null, new object[] { obj3 }, null, true);
                         }
                     }
                     else
//.........这里部分代码省略.........
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:101,代码来源:CustomAttribute.cs


注:本文中的System.Reflection.RuntimeModule.GetRuntimeAssembly方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。