Esempio n. 1
0
 private ParameterInfo(ParameterInfo accessor, MemberInfo member)
 {
     this.MemberImpl = member;
     this.NameImpl = accessor.Name;
     this.m_nameIsCached = true;
     this.ClassImpl = accessor.ParameterType;
     this.PositionImpl = accessor.Position;
     this.AttrsImpl = accessor.Attributes;
     this.m_tkParamDef = System.Reflection.MetadataToken.IsNullToken(accessor.MetadataToken) ? 0x8000000 : accessor.MetadataToken;
     this.m_scope = accessor.m_scope;
 }
Esempio n. 2
0
 private ParameterInfo(Signature signature, MetadataImport scope, int tkParamDef, int position, ParameterAttributes attributes, MemberInfo member)
 {
     this.PositionImpl = position;
     this.MemberImpl = member;
     this.m_signature = signature;
     this.m_tkParamDef = System.Reflection.MetadataToken.IsNullToken(tkParamDef) ? 0x8000000 : tkParamDef;
     this.m_scope = scope;
     this.AttrsImpl = attributes;
     this.ClassImpl = null;
     this.NameImpl = null;
 }
 private RuntimeParameterInfo(RuntimeParameterInfo accessor, MemberInfo member)
 {
     base.MemberImpl = member;
     this.m_originalMember = accessor.MemberImpl as MethodBase;
     base.NameImpl = accessor.Name;
     this.m_nameIsCached = true;
     base.ClassImpl = accessor.ParameterType;
     base.PositionImpl = accessor.Position;
     base.AttrsImpl = accessor.Attributes;
     this.m_tkParamDef = System.Reflection.MetadataToken.IsNullToken(accessor.MetadataToken) ? 0x8000000 : accessor.MetadataToken;
     this.m_scope = accessor.m_scope;
 }
Esempio n. 4
0
 internal ParameterInfo(MethodInfo owner, string name, RuntimeType parameterType, int position)
 {
     this.MemberImpl = owner;
     this.NameImpl = name;
     this.m_nameIsCached = true;
     this.m_noDefaultValue = true;
     this.ClassImpl = parameterType;
     this.PositionImpl = position;
     this.AttrsImpl = ParameterAttributes.None;
     this.m_tkParamDef = 0x8000000;
     this.m_scope = MetadataImport.EmptyImport;
 }
Esempio n. 5
0
 private bool Equals(MetadataImport import)
 {
     return(import.m_metadataImport2 == m_metadataImport2);
 }
Esempio n. 6
0
        public override FieldInfo?ResolveField(int metadataToken, Type[]?genericTypeArguments, Type[]?genericMethodArguments)
        {
            try
            {
                MetadataToken tk = new MetadataToken(metadataToken);

                if (!MetadataImport.IsValidToken(tk))
                {
                    throw new ArgumentOutOfRangeException(nameof(metadataToken),
                                                          SR.Format(SR.Argument_InvalidToken, tk, this));
                }

                RuntimeTypeHandle[]? typeArgs   = null;
                RuntimeTypeHandle[]? methodArgs = null;
                if (genericTypeArguments?.Length > 0)
                {
                    typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
                }
                if (genericMethodArguments?.Length > 0)
                {
                    methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
                }

                ModuleHandle moduleHandle = new ModuleHandle(this);
                if (!tk.IsFieldDef)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this),
                                                    nameof(metadataToken));
                    }

                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature != MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this),
                                                        nameof(metadataToken));
                        }
                    }
                }

                IRuntimeFieldInfo fieldHandle = moduleHandle.ResolveFieldHandle(metadataToken, typeArgs, methodArgs).GetRuntimeFieldInfo();

                RuntimeType declaringType = RuntimeFieldHandle.GetApproxDeclaringType(fieldHandle.Value);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    int tkDeclaringType = ModuleHandle.GetMetadataImport(this).GetParentToken(metadataToken);
                    declaringType = (RuntimeType)ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return(RuntimeType.GetFieldInfo(declaringType, fieldHandle));
            }
            catch (MissingFieldException)
            {
                return(ResolveLiteralField(metadataToken, genericTypeArguments, genericMethodArguments));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(SR.Argument_BadImageFormatExceptionResolve, e);
            }
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        internal static unsafe void AssignAssociates(
            MetadataImport scope,
            int mdPropEvent,
            RuntimeType declaringType,
            RuntimeType reflectedType,
            out RuntimeMethodInfo addOn,
            out RuntimeMethodInfo removeOn,
            out RuntimeMethodInfo fireOn,
            out RuntimeMethodInfo getter,
            out RuntimeMethodInfo setter,
            out MethodInfo[] other,
            out bool composedOfAllPrivateMethods,
            out BindingFlags bindingFlags)
        {
            addOn = removeOn = fireOn = getter = setter = null;

            Attributes attributes =
                Attributes.ComposedOfAllPrivateMethods |
                Attributes.ComposedOfAllVirtualMethods |
                Attributes.ComposedOfNoPublicMembers |
                Attributes.ComposedOfNoStaticMembers;

            while (RuntimeTypeHandle.IsGenericVariable(reflectedType))
            {
                reflectedType = (RuntimeType)reflectedType.BaseType;
            }

            bool isInherited = declaringType != reflectedType;

            List <MethodInfo> otherList = null;

            MetadataEnumResult associatesData;

            scope.Enum(MetadataTokenType.MethodDef, mdPropEvent, out associatesData);

            int cAssociates = associatesData.Length / 2;

            for (int i = 0; i < cAssociates; i++)
            {
                int methodDefToken = associatesData[i * 2];
                MethodSemanticsAttributes semantics = (MethodSemanticsAttributes)associatesData[i * 2 + 1];

                #region Assign each associate
                RuntimeMethodInfo associateMethod =
                    AssignAssociates(methodDefToken, declaringType, reflectedType);

                if (associateMethod == null)
                {
                    continue;
                }

                MethodAttributes methAttr  = associateMethod.Attributes;
                bool             isPrivate = (methAttr & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
                bool             isVirtual = (methAttr & MethodAttributes.Virtual) != 0;

                MethodAttributes visibility = methAttr & MethodAttributes.MemberAccessMask;
                bool             isPublic   = visibility == MethodAttributes.Public;
                bool             isStatic   = (methAttr & MethodAttributes.Static) != 0;

                if (isPublic)
                {
                    attributes &= ~Attributes.ComposedOfNoPublicMembers;
                    attributes &= ~Attributes.ComposedOfAllPrivateMethods;
                }
                else if (!isPrivate)
                {
                    attributes &= ~Attributes.ComposedOfAllPrivateMethods;
                }

                if (isStatic)
                {
                    attributes &= ~Attributes.ComposedOfNoStaticMembers;
                }

                if (!isVirtual)
                {
                    attributes &= ~Attributes.ComposedOfAllVirtualMethods;
                }
                #endregion

                if (semantics == MethodSemanticsAttributes.Setter)
                {
                    setter = associateMethod;
                }
                else if (semantics == MethodSemanticsAttributes.Getter)
                {
                    getter = associateMethod;
                }
                else if (semantics == MethodSemanticsAttributes.Fire)
                {
                    fireOn = associateMethod;
                }
                else if (semantics == MethodSemanticsAttributes.AddOn)
                {
                    addOn = associateMethod;
                }
                else if (semantics == MethodSemanticsAttributes.RemoveOn)
                {
                    removeOn = associateMethod;
                }
                else
                {
                    if (otherList == null)
                    {
                        otherList = new List <MethodInfo>(cAssociates);
                    }
                    otherList.Add(associateMethod);
                }
            }

            bool isPseudoPublic = (attributes & Attributes.ComposedOfNoPublicMembers) == 0;
            bool isPseudoStatic = (attributes & Attributes.ComposedOfNoStaticMembers) == 0;
            bindingFlags = RuntimeType.FilterPreCalculate(isPseudoPublic, isInherited, isPseudoStatic);

            composedOfAllPrivateMethods = (attributes & Attributes.ComposedOfAllPrivateMethods) != 0;

            other = (otherList != null) ? otherList.ToArray() : null;
        }
Esempio n. 9
0
        private RuntimeParameterInfo(RuntimeParameterInfo accessor, MemberInfo member)
        {
            // Change ownership
            MemberImpl = member;
            
            // The original owner should always be a method, because this method is only used to 
            // change the owner from a method to a property.
            m_originalMember = accessor.MemberImpl as MethodBase;
            Contract.Assert(m_originalMember != null);

            // Populate all the caches -- we inherit this behavior from RTM
            NameImpl = accessor.Name;
            m_nameIsCached = true;
            ClassImpl = accessor.ParameterType;
            PositionImpl = accessor.Position;
            AttrsImpl = accessor.Attributes;

            // Strictly speeking, property's don't contain paramter tokens
            // However we need this to make ca's work... oh well...
            m_tkParamDef = MdToken.IsNullToken(accessor.MetadataToken) ? (int)MetadataTokenType.ParamDef : accessor.MetadataToken;
            m_scope = accessor.m_scope;
        }
Esempio n. 10
0
        public override MemberInfo?ResolveMember(
            int metadataToken,
            Type[]?genericTypeArguments,
            Type[]?genericMethodArguments
            )
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (tk.IsProperty)
            {
                throw new ArgumentException(SR.InvalidOperation_PropertyInfoNotAvailable);
            }

            if (tk.IsEvent)
            {
                throw new ArgumentException(SR.InvalidOperation_EventInfoNotAvailable);
            }

            if (tk.IsMethodSpec || tk.IsMethodDef)
            {
                return(ResolveMethod(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsFieldDef)
            {
                return(ResolveField(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsTypeRef || tk.IsTypeDef || tk.IsTypeSpec)
            {
                return(ResolveType(metadataToken, genericTypeArguments, genericMethodArguments));
            }

            if (tk.IsMemberRef)
            {
                if (!MetadataImport.IsValidToken(tk))
                {
                    throw new ArgumentOutOfRangeException(
                              nameof(metadataToken),
                              SR.Format(SR.Argument_InvalidToken, tk, this)
                              );
                }

                ConstArray sig = MetadataImport.GetMemberRefProps(tk);
                unsafe
                {
                    if (*(MdSigCallingConvention *)sig.Signature == MdSigCallingConvention.Field)
                    {
                        return(ResolveField(tk, genericTypeArguments, genericMethodArguments));
                    }
                    else
                    {
                        return(ResolveMethod(tk, genericTypeArguments, genericMethodArguments));
                    }
                }
            }

            throw new ArgumentException(
                      SR.Format(SR.Argument_ResolveMember, tk, this),
                      nameof(metadataToken)
                      );
        }
Esempio n. 11
0
        private ParameterInfo(ParameterInfo accessor, MemberInfo member)
        {
            // Change ownership
            MemberImpl = member;

            // Populate all the caches -- we inherit this behavior from RTM
            NameImpl = accessor.Name; 
            m_nameIsCached = true;
            ClassImpl = accessor.ParameterType;
            PositionImpl = accessor.Position;
            AttrsImpl = accessor.Attributes;

            // Strictly speeking, property's don't contain paramter tokens
            // However we need this to make ca's work... oh well...
            m_tkParamDef = MdToken.IsNullToken(accessor.MetadataToken) ? (int)MetadataTokenType.ParamDef : accessor.MetadataToken;
            m_scope = accessor.m_scope;
        }
 public void GetClassLayout(int typeTokenDef, out int packSize, out int classSize)
 {
     MetadataImport._GetClassLayout(this.m_metadataImport2, typeTokenDef, out packSize, out classSize);
 }
Esempio n. 13
0
        public static unsafe Object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = 0;
            long buffer = 0;
            int length;

            scope.GetDefaultValue(token, out buffer, out length, out corElementType);

            Type fieldType = Type.GetTypeFromHandle(fieldTypeHandle);

            if (fieldType.IsEnum && raw == false)
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch
                    case CorElementType.Void:
                        return DBNull.Value;

                    case CorElementType.Char:
                        defaultValue = *(char*)&buffer;
                        break;

                    case CorElementType.I1:
                        defaultValue = *(sbyte*)&buffer;
                        break;

                    case CorElementType.U1:
                        defaultValue = *(byte*)&buffer;
                        break;

                    case CorElementType.I2:
                        defaultValue = *(short*)&buffer;
                        break;

                    case CorElementType.U2:
                        defaultValue = *(ushort*)&buffer;
                        break;

                    case CorElementType.I4:
                        defaultValue = *(int*)&buffer;
                        break;

                    case CorElementType.U4:
                        defaultValue = *(uint*)&buffer;
                        break;

                    case CorElementType.I8:
                        defaultValue = buffer;
                        break;

                    case CorElementType.U8:
                        defaultValue = buffer;
                        break;
                
                    default:
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }

                return RuntimeType.CreateEnum(fieldTypeHandle, defaultValue);                       
            }
            else if (fieldType == typeof(DateTime))
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch
                    case CorElementType.Void:
                        return DBNull.Value;
                        
                    case CorElementType.I8:
                        defaultValue = buffer;
                        break;

                    case CorElementType.U8:
                        defaultValue = buffer;
                        break;
                
                    default:
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion  
                }

                return new DateTime(defaultValue);
            }
            else
            {
                switch (corElementType)
                {
                    #region Switch
                    case CorElementType.Void:
                        return DBNull.Value;

                    case CorElementType.Char:
                        return *(char*)&buffer;

                    case CorElementType.I1:
                        return *(sbyte*)&buffer;

                    case CorElementType.U1:
                        return *(byte*)&buffer;

                    case CorElementType.I2:
                        return *(short*)&buffer;

                    case CorElementType.U2:
                        return *(ushort*)&buffer;

                    case CorElementType.I4:
                        return *(int*)&buffer;

                    case CorElementType.U4:
                        return *(uint*)&buffer;

                    case CorElementType.I8:
                        return buffer;

                    case CorElementType.U8:
                        return (ulong)buffer;
                
                    case CorElementType.Boolean :
                        return (*(byte*)&buffer != 0);

                    case CorElementType.R4 :
                        return *(float*)&buffer;

                    case CorElementType.R8:
                        return *(double*)&buffer;

                    case CorElementType.String:
                        return new String((char*)buffer, 0, length/2);

                    case CorElementType.Class:
                        return null;
                    
                    default:
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }
            }
        } 
Esempio n. 14
0
        private void OnDeserialized(StreamingContext context)
        {
            // Once all the serializable fields have come in we can setup this
            // instance based on just two of them (MemberImpl and PositionImpl).
            // Use these members to lookup a template ParameterInfo then clone
            // that instance into this one.

            ParameterInfo targetInfo = null;

            if (MemberImpl == null) 
                throw new SerializationException(Environment.GetResourceString(ResId.Serialization_InsufficientState));
    
            ParameterInfo[] args = null;

            switch (MemberImpl.MemberType) 
            {
                case MemberTypes.Constructor:
                case MemberTypes.Method:
                    if (PositionImpl == -1)
                    {
                        if (MemberImpl.MemberType == MemberTypes.Method)
                            targetInfo = ((MethodInfo)MemberImpl).ReturnParameter;
                        else
                            throw new SerializationException(Environment.GetResourceString(ResId.Serialization_BadParameterInfo));
                    }
                    else
                    {
                        args = ((MethodBase)MemberImpl).GetParametersNoCopy();

                        if (args != null && PositionImpl < args.Length) 
                            targetInfo = args[PositionImpl];
                        else
                            throw new SerializationException(Environment.GetResourceString(ResId.Serialization_BadParameterInfo));
                    }
                    break;

                case MemberTypes.Property:
                    args = ((PropertyInfo)MemberImpl).GetIndexParameters();

                    if (args != null && PositionImpl > -1 && PositionImpl < args.Length) 
                        targetInfo = args[PositionImpl];
                    else
                        throw new SerializationException(Environment.GetResourceString(ResId.Serialization_BadParameterInfo));
                    break;

                default:
                    throw new SerializationException(Environment.GetResourceString(ResId.Serialization_NoParameterInfo));
            }   

            // We've got a ParameterInfo that matches the incoming information,
            // clone it into ourselves. We really only need to copy the private
            // members we didn't receive via serialization.
            ASSERT.PRECONDITION(targetInfo != null);

            m_tkParamDef = targetInfo.m_tkParamDef;
            m_scope = targetInfo.m_scope;
            m_signature = targetInfo.m_signature;
            m_nameIsCached = true;
        }
 public void Enum(MetadataTokenType type, int parent, out MetadataEnumResult result)
 {
     MetadataImport._Enum(this.m_metadataImport2, (int)type, parent, out result);
 }
 public bool IsValidToken(int token)
 {
     return(MetadataImport._IsValidToken(this.m_metadataImport2, token));
 }
 public bool GetFieldOffset(int typeTokenDef, int fieldTokenDef, out int offset)
 {
     return(MetadataImport._GetFieldOffset(this.m_metadataImport2, typeTokenDef, fieldTokenDef, out offset));
 }
Esempio n. 18
0
 internal static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out RuntimeMethodHandle ctor, out bool ctorHasParameters, out bool isVarArg)
 {
     ctor = new RuntimeMethodHandle();
     attributeType = null;
     ctorHasParameters = false;
     isVarArg = false;
     IntPtr ptr1 = (IntPtr) (((void*) caRecord.blob.Signature) + caRecord.blob.Length);
     attributeType = decoratedModule.ResolveType(scope.GetParentToken((int) caRecord.tkCtor), null, null) as RuntimeType;
     if (!attributeFilterType.IsAssignableFrom(attributeType))
     {
         return false;
     }
     if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
     {
         return false;
     }
     if ((attributeType.Assembly != lastAptcaOkAssembly) && !attributeType.Assembly.AptcaCheck(decoratedModule.Assembly))
     {
         return false;
     }
     lastAptcaOkAssembly = decoratedModule.Assembly;
     ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor);
     isVarArg = (methodSignature[0] & 5) != 0;
     ctorHasParameters = methodSignature[1] != 0;
     if (ctorHasParameters)
     {
         ctor = decoratedModule.ModuleHandle.ResolveMethodHandle((int) caRecord.tkCtor);
     }
     else
     {
         ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();
         if (ctor.IsNullHandle() && !attributeType.IsValueType)
         {
             throw new MissingMethodException(".ctor");
         }
     }
     if (ctor.IsNullHandle())
     {
         if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule.ModuleHandle))
         {
             return false;
         }
         return true;
     }
     if (ctor.IsVisibleFromModule(decoratedModule))
     {
         return true;
     }
     MetadataToken token = new MetadataToken();
     if (decoratedToken.IsParamDef)
     {
         token = new MetadataToken(scope.GetParentToken((int) decoratedToken));
         token = new MetadataToken(scope.GetParentToken((int) token));
     }
     else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef))
     {
         token = new MetadataToken(scope.GetParentToken((int) decoratedToken));
     }
     else if (decoratedToken.IsTypeDef)
     {
         token = decoratedToken;
     }
     return (token.IsTypeDef && ctor.IsVisibleFromType(decoratedModule.ModuleHandle.ResolveTypeHandle((int) token)));
 }
Esempio n. 19
0
        private ParameterInfo(
            Signature signature, MetadataImport scope, int tkParamDef, 
            int position, ParameterAttributes attributes, MemberInfo member) 
        {
            ASSERT.PRECONDITION(member != null);
            ASSERT.PRECONDITION(LOGIC.BIJECTION(MdToken.IsNullToken(tkParamDef), scope.Equals(null)));
            ASSERT.PRECONDITION(LOGIC.IMPLIES(!MdToken.IsNullToken(tkParamDef), 
                MdToken.IsTokenOfType(tkParamDef, MetadataTokenType.ParamDef)));

            PositionImpl = position;
            MemberImpl = member;
            m_signature = signature;
            m_tkParamDef = MdToken.IsNullToken(tkParamDef) ? (int)MetadataTokenType.ParamDef : tkParamDef;
            m_scope = scope;
            AttrsImpl = attributes;

            ClassImpl = null;
            NameImpl = null;
        }
        internal static void AssignAssociates(MetadataImport scope, int mdPropEvent, RuntimeType declaringType, RuntimeType reflectedType, out RuntimeMethodInfo addOn, out RuntimeMethodInfo removeOn, out RuntimeMethodInfo fireOn, out RuntimeMethodInfo getter, out RuntimeMethodInfo setter, out MethodInfo[] other, out bool composedOfAllPrivateMethods, out BindingFlags bindingFlags)
        {
            RuntimeMethodInfo runtimeMethodInfo;

            setter   = (runtimeMethodInfo = null);
            getter   = (runtimeMethodInfo = runtimeMethodInfo);
            fireOn   = (runtimeMethodInfo = runtimeMethodInfo);
            removeOn = (runtimeMethodInfo = runtimeMethodInfo);
            addOn    = runtimeMethodInfo;
            Associates.Attributes attributes = Associates.Attributes.ComposedOfAllVirtualMethods | Associates.Attributes.ComposedOfAllPrivateMethods | Associates.Attributes.ComposedOfNoPublicMembers | Associates.Attributes.ComposedOfNoStaticMembers;
            while (RuntimeTypeHandle.IsGenericVariable(reflectedType))
            {
                reflectedType = (RuntimeType)reflectedType.BaseType;
            }
            bool isInherited        = declaringType != reflectedType;
            List <MethodInfo>  list = null;
            MetadataEnumResult metadataEnumResult;

            scope.Enum(MetadataTokenType.MethodDef, mdPropEvent, out metadataEnumResult);
            int num = metadataEnumResult.Length / 2;

            for (int i = 0; i < num; i++)
            {
                int tkMethod = metadataEnumResult[i * 2];
                MethodSemanticsAttributes methodSemanticsAttributes = (MethodSemanticsAttributes)metadataEnumResult[i * 2 + 1];
                RuntimeMethodInfo         runtimeMethodInfo2        = Associates.AssignAssociates(tkMethod, declaringType, reflectedType);
                if (!(runtimeMethodInfo2 == null))
                {
                    MethodAttributes attributes2      = runtimeMethodInfo2.Attributes;
                    bool             flag             = (attributes2 & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
                    bool             flag2            = (attributes2 & MethodAttributes.Virtual) > MethodAttributes.PrivateScope;
                    MethodAttributes methodAttributes = attributes2 & MethodAttributes.MemberAccessMask;
                    bool             flag3            = methodAttributes == MethodAttributes.Public;
                    bool             flag4            = (attributes2 & MethodAttributes.Static) > MethodAttributes.PrivateScope;
                    if (flag3)
                    {
                        attributes &= ~Associates.Attributes.ComposedOfNoPublicMembers;
                        attributes &= ~Associates.Attributes.ComposedOfAllPrivateMethods;
                    }
                    else if (!flag)
                    {
                        attributes &= ~Associates.Attributes.ComposedOfAllPrivateMethods;
                    }
                    if (flag4)
                    {
                        attributes &= ~Associates.Attributes.ComposedOfNoStaticMembers;
                    }
                    if (!flag2)
                    {
                        attributes &= ~Associates.Attributes.ComposedOfAllVirtualMethods;
                    }
                    if (methodSemanticsAttributes == MethodSemanticsAttributes.Setter)
                    {
                        setter = runtimeMethodInfo2;
                    }
                    else if (methodSemanticsAttributes == MethodSemanticsAttributes.Getter)
                    {
                        getter = runtimeMethodInfo2;
                    }
                    else if (methodSemanticsAttributes == MethodSemanticsAttributes.Fire)
                    {
                        fireOn = runtimeMethodInfo2;
                    }
                    else if (methodSemanticsAttributes == MethodSemanticsAttributes.AddOn)
                    {
                        addOn = runtimeMethodInfo2;
                    }
                    else if (methodSemanticsAttributes == MethodSemanticsAttributes.RemoveOn)
                    {
                        removeOn = runtimeMethodInfo2;
                    }
                    else
                    {
                        if (list == null)
                        {
                            list = new List <MethodInfo>(num);
                        }
                        list.Add(runtimeMethodInfo2);
                    }
                }
            }
            bool isPublic = (attributes & Associates.Attributes.ComposedOfNoPublicMembers) == (Associates.Attributes) 0;
            bool isStatic = (attributes & Associates.Attributes.ComposedOfNoStaticMembers) == (Associates.Attributes) 0;

            bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
            composedOfAllPrivateMethods = ((attributes & Associates.Attributes.ComposedOfAllPrivateMethods) > (Associates.Attributes) 0);
            other = ((list != null) ? list.ToArray() : null);
        }
Esempio n. 21
0
        internal static void AssignAssociates(MetadataImport scope, int mdPropEvent, RuntimeType declaringType, RuntimeType reflectedType, out RuntimeMethodInfo addOn, out RuntimeMethodInfo removeOn, out RuntimeMethodInfo fireOn, out RuntimeMethodInfo getter, out RuntimeMethodInfo setter, out MethodInfo[] other, out bool composedOfAllPrivateMethods, out BindingFlags bindingFlags)
        {
            addOn = removeOn = fireOn = getter = setter = (RuntimeMethodInfo)null;
            Associates.Attributes attributes1 = Associates.Attributes.ComposedOfAllVirtualMethods | Associates.Attributes.ComposedOfAllPrivateMethods | Associates.Attributes.ComposedOfNoPublicMembers | Associates.Attributes.ComposedOfNoStaticMembers;
            while (RuntimeTypeHandle.IsGenericVariable(reflectedType))
            {
                reflectedType = (RuntimeType)reflectedType.BaseType;
            }
            bool isInherited = declaringType != reflectedType;
            List <MethodInfo>  methodInfoList = (List <MethodInfo>)null;
            MetadataEnumResult result;

            scope.Enum(MetadataTokenType.MethodDef, mdPropEvent, out result);
            int capacity = result.Length / 2;

            for (int index = 0; index < capacity; ++index)
            {
                int tkMethod = result[index * 2];
                MethodSemanticsAttributes semanticsAttributes = (MethodSemanticsAttributes)result[index * 2 + 1];
                RuntimeType       declaredType      = declaringType;
                RuntimeType       reflectedType1    = reflectedType;
                RuntimeMethodInfo runtimeMethodInfo = Associates.AssignAssociates(tkMethod, declaredType, reflectedType1);
                if (!((MethodInfo)runtimeMethodInfo == (MethodInfo)null))
                {
                    MethodAttributes attributes2 = runtimeMethodInfo.Attributes;
                    bool             flag1       = (attributes2 & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
                    bool             flag2       = (uint)(attributes2 & MethodAttributes.Virtual) > 0U;
                    int  num   = (attributes2 & MethodAttributes.MemberAccessMask) == MethodAttributes.Public ? 1 : 0;
                    bool flag3 = (uint)(attributes2 & MethodAttributes.Static) > 0U;
                    if (num != 0)
                    {
                        attributes1 = attributes1 & ~Associates.Attributes.ComposedOfNoPublicMembers & ~Associates.Attributes.ComposedOfAllPrivateMethods;
                    }
                    else if (!flag1)
                    {
                        attributes1 &= ~Associates.Attributes.ComposedOfAllPrivateMethods;
                    }
                    if (flag3)
                    {
                        attributes1 &= ~Associates.Attributes.ComposedOfNoStaticMembers;
                    }
                    if (!flag2)
                    {
                        attributes1 &= ~Associates.Attributes.ComposedOfAllVirtualMethods;
                    }
                    if (semanticsAttributes == MethodSemanticsAttributes.Setter)
                    {
                        setter = runtimeMethodInfo;
                    }
                    else if (semanticsAttributes == MethodSemanticsAttributes.Getter)
                    {
                        getter = runtimeMethodInfo;
                    }
                    else if (semanticsAttributes == MethodSemanticsAttributes.Fire)
                    {
                        fireOn = runtimeMethodInfo;
                    }
                    else if (semanticsAttributes == MethodSemanticsAttributes.AddOn)
                    {
                        addOn = runtimeMethodInfo;
                    }
                    else if (semanticsAttributes == MethodSemanticsAttributes.RemoveOn)
                    {
                        removeOn = runtimeMethodInfo;
                    }
                    else
                    {
                        if (methodInfoList == null)
                        {
                            methodInfoList = new List <MethodInfo>(capacity);
                        }
                        methodInfoList.Add((MethodInfo)runtimeMethodInfo);
                    }
                }
            }
            bool isPublic = (attributes1 & Associates.Attributes.ComposedOfNoPublicMembers) == (Associates.Attributes) 0;
            bool isStatic = (attributes1 & Associates.Attributes.ComposedOfNoStaticMembers) == (Associates.Attributes) 0;

            bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
            composedOfAllPrivateMethods = (uint)(attributes1 & Associates.Attributes.ComposedOfAllPrivateMethods) > 0U;
            other = methodInfoList != null?methodInfoList.ToArray() : (MethodInfo[])null;
        }
 public void GetScopeProps(out Guid mvid)
 {
     MetadataImport._GetScopeProps(this.m_metadataImport2, out mvid);
 }
Esempio n. 23
0
 internal bool Equals(MetadataImport import)
 {
     return(import.m_metadataImport2 == this.m_metadataImport2);
 }
Esempio n. 24
0
 // ctor for no metadata MethodInfo in the DynamicMethod and RuntimeMethodInfo cases
 internal RuntimeParameterInfo(MethodInfo owner, String name, Type parameterType, int position)
 {
     MemberImpl = owner;
     NameImpl = name;
     m_nameIsCached = true;
     m_noMetadata = true;
     ClassImpl = parameterType;
     PositionImpl = position;
     AttrsImpl = ParameterAttributes.None;
     m_tkParamDef = (int)MetadataTokenType.ParamDef;
     m_scope = MetadataImport.EmptyImport;
 }
Esempio n. 25
0
        public unsafe static object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = CorElementType.End;
            long           num            = 0L;
            int            num2;
            string         defaultValue = scope.GetDefaultValue(token, out num, out num2, out corElementType);
            RuntimeType    runtimeType  = fieldTypeHandle.GetRuntimeType();

            if (runtimeType.IsEnum && !raw)
            {
                long value;
                switch (corElementType)
                {
                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    value = (long)((ulong)(*(ushort *)(&num)));
                    goto IL_C8;

                case CorElementType.I1:
                    value = (long)(*(sbyte *)(&num));
                    goto IL_C8;

                case CorElementType.U1:
                    value = (long)((ulong)(*(byte *)(&num)));
                    goto IL_C8;

                case CorElementType.I2:
                    value = (long)(*(short *)(&num));
                    goto IL_C8;

                case CorElementType.U2:
                    value = (long)((ulong)(*(ushort *)(&num)));
                    goto IL_C8;

                case CorElementType.I4:
                    value = (long)(*(int *)(&num));
                    goto IL_C8;

                case CorElementType.U4:
                    value = (long)((ulong)(*(uint *)(&num)));
                    goto IL_C8;

                case CorElementType.I8:
                    value = num;
                    goto IL_C8;

                case CorElementType.U8:
                    value = num;
                    goto IL_C8;
                }
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
IL_C8:
                return(RuntimeType.CreateEnum(runtimeType, value));
            }
            if (!(runtimeType == typeof(DateTime)))
            {
                switch (corElementType)
                {
                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Boolean:
                    return(*(int *)(&num) != 0);

                case CorElementType.Char:
                    return((char)(*(ushort *)(&num)));

                case CorElementType.I1:
                    return(*(sbyte *)(&num));

                case CorElementType.U1:
                    return(*(byte *)(&num));

                case CorElementType.I2:
                    return(*(short *)(&num));

                case CorElementType.U2:
                    return(*(ushort *)(&num));

                case CorElementType.I4:
                    return(*(int *)(&num));

                case CorElementType.U4:
                    return(*(uint *)(&num));

                case CorElementType.I8:
                    return(num);

                case CorElementType.U8:
                    return((ulong)num);

                case CorElementType.R4:
                    return(*(float *)(&num));

                case CorElementType.R8:
                    return(*(double *)(&num));

                case CorElementType.String:
                    if (defaultValue != null)
                    {
                        return(defaultValue);
                    }
                    return(string.Empty);

                case CorElementType.Class:
                    return(null);
                }
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
            }
            if (corElementType != CorElementType.Void)
            {
                long ticks;
                if (corElementType != CorElementType.I8)
                {
                    if (corElementType != CorElementType.U8)
                    {
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    }
                    ticks = num;
                }
                else
                {
                    ticks = num;
                }
                return(new DateTime(ticks));
            }
            return(DBNull.Value);
        }
        private unsafe static bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg)
        {
            ctor              = null;
            attributeType     = null;
            ctorHasParameters = false;
            isVarArg          = false;
            IntPtr signature = caRecord.blob.Signature;
            IntPtr intPtr    = (IntPtr)((void *)((byte *)((void *)signature) + caRecord.blob.Length));

            attributeType = (decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType);
            if (!attributeFilterType.IsAssignableFrom(attributeType))
            {
                return(false);
            }
            if (!CustomAttribute.AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
            {
                return(false);
            }
            if ((attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime)
            {
                return(false);
            }
            RuntimeAssembly runtimeAssembly  = (RuntimeAssembly)attributeType.Assembly;
            RuntimeAssembly runtimeAssembly2 = (RuntimeAssembly)decoratedModule.Assembly;

            if (runtimeAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(runtimeAssembly, runtimeAssembly2))
            {
                return(false);
            }
            lastAptcaOkAssembly = runtimeAssembly2;
            ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor);

            isVarArg          = ((methodSignature[0] & 5) > 0);
            ctorHasParameters = (methodSignature[1] > 0);
            if (ctorHasParameters)
            {
                ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor);
            }
            else
            {
                ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();
                if (ctor == null && !attributeType.IsValueType)
                {
                    throw new MissingMethodException(".ctor");
                }
            }
            MetadataToken token = default(MetadataToken);

            if (decoratedToken.IsParamDef)
            {
                token = new MetadataToken(scope.GetParentToken(decoratedToken));
                token = new MetadataToken(scope.GetParentToken(token));
            }
            else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef)
            {
                token = new MetadataToken(scope.GetParentToken(decoratedToken));
            }
            else if (decoratedToken.IsTypeDef)
            {
                token = decoratedToken;
            }
            else if (decoratedToken.IsGenericPar)
            {
                token = new MetadataToken(scope.GetParentToken(decoratedToken));
                if (token.IsMethodDef)
                {
                    token = new MetadataToken(scope.GetParentToken(token));
                }
            }
            RuntimeTypeHandle sourceTypeHandle = token.IsTypeDef ? decoratedModule.ModuleHandle.ResolveTypeHandle(token) : default(RuntimeTypeHandle);

            return(RuntimeMethodHandle.IsCAVisibleFromDecoratedType(attributeType.TypeHandle, ctor, sourceTypeHandle, decoratedModule));
        }
        private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg)
        {
            ctor              = null;
            attributeType     = null;
            ctorHasParameters = false;
            isVarArg          = false;
            IntPtr ptr1 = (IntPtr)(((void *)caRecord.blob.Signature) + caRecord.blob.Length);

            attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), null, null) as RuntimeType;
            if (!attributeFilterType.IsAssignableFrom(attributeType))
            {
                return(false);
            }
            if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
            {
                return(false);
            }
            RuntimeAssembly targetAssembly = (RuntimeAssembly)attributeType.Assembly;
            RuntimeAssembly assembly       = (RuntimeAssembly)decoratedModule.Assembly;

            if ((targetAssembly != lastAptcaOkAssembly) && !RuntimeAssembly.AptcaCheck(targetAssembly, assembly))
            {
                return(false);
            }
            lastAptcaOkAssembly = assembly;
            ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor);

            isVarArg          = (methodSignature[0] & 5) != 0;
            ctorHasParameters = methodSignature[1] != 0;
            if (ctorHasParameters)
            {
                ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int)caRecord.tkCtor);
            }
            else
            {
                ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();
                if ((ctor == null) && !attributeType.IsValueType)
                {
                    throw new MissingMethodException(".ctor");
                }
            }
            if (ctor == null)
            {
                if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule))
                {
                    return(false);
                }
                return(true);
            }
            if (RuntimeMethodHandle.IsVisibleFromModule(ctor, decoratedModule))
            {
                return(true);
            }
            MetadataToken token = new MetadataToken();

            if (decoratedToken.IsParamDef)
            {
                token = new MetadataToken(scope.GetParentToken((int)decoratedToken));
                token = new MetadataToken(scope.GetParentToken((int)token));
            }
            else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef))
            {
                token = new MetadataToken(scope.GetParentToken((int)decoratedToken));
            }
            else if (decoratedToken.IsTypeDef)
            {
                token = decoratedToken;
            }
            return(token.IsTypeDef && RuntimeMethodHandle.IsVisibleFromType(ctor, decoratedModule.ModuleHandle.ResolveTypeHandle((int)token)));
        }
Esempio n. 28
0
        private static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out IRuntimeMethodInfo ctor, out bool ctorHasParameters, out bool isVarArg)
        {
            ctor              = (IRuntimeMethodInfo)null;
            attributeType     = (RuntimeType)null;
            ctorHasParameters = false;
            isVarArg          = false;
            IntPtr num = (IntPtr)((void *)((IntPtr)(void *)caRecord.blob.Signature + caRecord.blob.Length));

            attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), (Type[])null, (Type[])null) as RuntimeType;
            if (!attributeFilterType.IsAssignableFrom((TypeInfo)attributeType) || !CustomAttribute.AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes) || (attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime)
            {
                return(false);
            }
            RuntimeAssembly targetAssembly = (RuntimeAssembly)attributeType.Assembly;
            RuntimeAssembly sourceAssembly = (RuntimeAssembly)decoratedModule.Assembly;

            if ((Assembly)targetAssembly != lastAptcaOkAssembly && !RuntimeAssembly.AptcaCheck(targetAssembly, sourceAssembly))
            {
                return(false);
            }
            lastAptcaOkAssembly = (Assembly)sourceAssembly;
            ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor);

            isVarArg          = ((uint)methodSignature[0] & 5U) > 0U;
            ctorHasParameters = (uint)methodSignature[1] > 0U;
            RuntimeTypeHandle runtimeTypeHandle1;

            if (ctorHasParameters)
            {
                ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), (int)caRecord.tkCtor);
            }
            else
            {
                // ISSUE: explicit reference operation
                // ISSUE: variable of a reference type
                IRuntimeMethodInfo& local = @ctor;
                runtimeTypeHandle1 = attributeType.GetTypeHandleInternal();
                IRuntimeMethodInfo defaultConstructor = runtimeTypeHandle1.GetDefaultConstructor();
        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
                            {
                                RtFieldInfo field = attributeType.GetField(str) as RtFieldInfo;
                                if (isDecoratedTargetSecurityTransparent)
                                {
                                    RuntimeFieldHandle.CheckAttributeAccess(field.FieldHandle, decoratedModule.GetNativeHandle());
                                }
                                field.InternalSetValue(obj2, obj3, BindingFlags.Default, Type.DefaultBinder, null, false);
                            }
                        }
                        catch (Exception exception)
                        {
                            throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }), exception);
                        }
                    }
                    if (!signature.Equals(blobEnd))
                    {
                        throw new CustomAttributeFormatException();
                    }
                    attributes[length++] = obj2;
                }
            }
            frame.Pop();
            if ((length == customAttributeRecords.Length) && (pcaCount == 0))
            {
                return(attributes);
            }
            object[] destinationArray = CreateAttributeArrayHelper(elementType, length + pcaCount);
            Array.Copy(attributes, 0, destinationArray, 0, length);
            return(destinationArray);
        }
 private bool Equals(MetadataImport import)
 {
     return (import.m_metadataImport2 == this.m_metadataImport2);
 }
        public static unsafe object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            int num2;
            long num4;
            CorElementType end = CorElementType.End;
            long num = 0L;
            string str = scope.GetDefaultValue(token, out num, out num2, out end);
            RuntimeType runtimeType = fieldTypeHandle.GetRuntimeType();
            if (!runtimeType.IsEnum || raw)
            {
                if (runtimeType == typeof(DateTime))
                {
                    num4 = 0L;
                    switch (end)
                    {
                        case CorElementType.I8:
                            num4 = num;
                            goto Label_0129;

                        case CorElementType.U8:
                            num4 = num;
                            goto Label_0129;

                        case CorElementType.Void:
                            return DBNull.Value;
                    }
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                }
                switch (end)
                {
                    case CorElementType.Void:
                        return DBNull.Value;

                    case CorElementType.Boolean:
                        return (*(((int*) &num)) != 0);

                    case CorElementType.Char:
                        return (char) *(((ushort*) &num));

                    case CorElementType.I1:
                        return *(((sbyte*) &num));

                    case CorElementType.U1:
                        return *(((byte*) &num));

                    case CorElementType.I2:
                        return *(((short*) &num));

                    case CorElementType.U2:
                        return *(((ushort*) &num));

                    case CorElementType.I4:
                        return *(((int*) &num));

                    case CorElementType.U4:
                        return *(((uint*) &num));

                    case CorElementType.I8:
                        return num;

                    case CorElementType.U8:
                        return (ulong) num;

                    case CorElementType.R4:
                        return *(((float*) &num));

                    case CorElementType.R8:
                        return *(((double*) &num));

                    case CorElementType.String:
                        if (str == null)
                        {
                            return string.Empty;
                        }
                        return str;

                    case CorElementType.Class:
                        return null;
                }
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
            }
            long num3 = 0L;
            switch (end)
            {
                case CorElementType.Void:
                    return DBNull.Value;

                case CorElementType.Char:
                    num3 = *((ushort*) &num);
                    break;

                case CorElementType.I1:
                    num3 = *((sbyte*) &num);
                    break;

                case CorElementType.U1:
                    num3 = *((byte*) &num);
                    break;

                case CorElementType.I2:
                    num3 = *((short*) &num);
                    break;

                case CorElementType.U2:
                    num3 = *((ushort*) &num);
                    break;

                case CorElementType.I4:
                    num3 = *((int*) &num);
                    break;

                case CorElementType.U4:
                    num3 = *((uint*) &num);
                    break;

                case CorElementType.I8:
                    num3 = num;
                    break;

                case CorElementType.U8:
                    num3 = num;
                    break;

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
            }
            return RuntimeType.CreateEnum(runtimeType, num3);
        Label_0129:
            return new DateTime(num4);
        }
Esempio n. 32
0
        public static unsafe Object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = 0;
            long           buffer         = 0;
            int            length;
            String         stringVal;

            stringVal = scope.GetDefaultValue(token, out buffer, out length, out corElementType);

            RuntimeType fieldType = fieldTypeHandle.GetRuntimeType();

            if (fieldType.IsEnum && raw == false)
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    defaultValue = *(char *)&buffer;
                    break;

                case CorElementType.I1:
                    defaultValue = *(sbyte *)&buffer;
                    break;

                case CorElementType.U1:
                    defaultValue = *(byte *)&buffer;
                    break;

                case CorElementType.I2:
                    defaultValue = *(short *)&buffer;
                    break;

                case CorElementType.U2:
                    defaultValue = *(ushort *)&buffer;
                    break;

                case CorElementType.I4:
                    defaultValue = *(int *)&buffer;
                    break;

                case CorElementType.U4:
                    defaultValue = *(uint *)&buffer;
                    break;

                case CorElementType.I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.U8:
                    defaultValue = buffer;
                    break;

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }

                return(RuntimeType.CreateEnum(fieldType, defaultValue));
            }
            else if (fieldType == typeof(DateTime))
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.U8:
                    defaultValue = buffer;
                    break;

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }

                return(new DateTime(defaultValue));
            }
            else
            {
                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    return(*(char *)&buffer);

                case CorElementType.I1:
                    return(*(sbyte *)&buffer);

                case CorElementType.U1:
                    return(*(byte *)&buffer);

                case CorElementType.I2:
                    return(*(short *)&buffer);

                case CorElementType.U2:
                    return(*(ushort *)&buffer);

                case CorElementType.I4:
                    return(*(int *)&buffer);

                case CorElementType.U4:
                    return(*(uint *)&buffer);

                case CorElementType.I8:
                    return(buffer);

                case CorElementType.U8:
                    return((ulong)buffer);

                case CorElementType.Boolean:
                    // The boolean value returned from the metadata engine is stored as a
                    // BOOL, which actually maps to an int. We need to read it out as an int
                    // to avoid problems on big-endian machines.
                    return(*(int *)&buffer != 0);

                case CorElementType.R4:
                    return(*(float *)&buffer);

                case CorElementType.R8:
                    return(*(double *)&buffer);

                case CorElementType.String:
                    // A string constant can be empty but never null.
                    // A nullref constant can only be type CorElementType.Class.
                    return(stringVal == null ? String.Empty : stringVal);

                case CorElementType.Class:
                    return(null);

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }
            }
        }
Esempio n. 33
0
 internal bool Equals(MetadataImport import)
 {
     return import.m_metadataImport2 == m_metadataImport2;
 }
Esempio n. 34
0
        internal static unsafe ParameterInfo[] GetParameters(
            IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo?returnParameter, bool fetchReturnParameter)
        {
            returnParameter = null;
            int sigArgCount = sig.Arguments.Length;

            ParameterInfo[] args = fetchReturnParameter ? null ! : new ParameterInfo[sigArgCount];

            int tkMethodDef = RuntimeMethodHandle.GetMethodDef(methodHandle);
            int cParamDefs  = 0;

            // Not all methods have tokens. Arrays, pointers and byRef types do not have tokens as they
            // are generated on the fly by the runtime.
            if (!MdToken.IsNullToken(tkMethodDef))
            {
                MetadataImport scope = RuntimeTypeHandle.GetMetadataImport(RuntimeMethodHandle.GetDeclaringType(methodHandle));

                MetadataEnumResult tkParamDefs;
                scope.EnumParams(tkMethodDef, out tkParamDefs);

                cParamDefs = tkParamDefs.Length;

                // Not all parameters have tokens. Parameters may have no token
                // if they have no name and no attributes.
                if (cParamDefs > sigArgCount + 1 /* return type */)
                {
                    throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
                }

                for (int i = 0; i < cParamDefs; i++)
                {
                    #region Populate ParameterInfos
                    ParameterAttributes attr;
                    int position, tkParamDef = tkParamDefs[i];

                    scope.GetParamDefProps(tkParamDef, out position, out attr);

                    position--;

                    if (fetchReturnParameter == true && position == -1)
                    {
                        // more than one return parameter?
                        if (returnParameter != null)
                        {
                            throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
                        }

                        returnParameter = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member);
                    }
                    else if (fetchReturnParameter == false && position >= 0)
                    {
                        // position beyong sigArgCount?
                        if (position >= sigArgCount)
                        {
                            throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
                        }

                        args[position] = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member);
                    }
                    #endregion
                }
            }

            // Fill in empty ParameterInfos for those without tokens
            if (fetchReturnParameter)
            {
                if (returnParameter == null)
                {
                    returnParameter = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, -1, (ParameterAttributes)0, member);
                }
            }
            else
            {
                if (cParamDefs < args.Length + 1)
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        if (args[i] != null)
                        {
                            continue;
                        }

                        args[i] = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, i, (ParameterAttributes)0, member);
                    }
                }
            }

            return(args);
        }
Esempio n. 35
0
 static MetadataImport()
 {
     EmptyImport = new MetadataImport(IntPtr.Zero);
     IID_IMetaDataImport = new Guid(0xd26df2ea, 0x7f58, 0x4183, 0x86, 190, 0x30, 0xae, 0x29, 0xa7, 0x5d, 0x8d);
     IID_IMetaDataAssemblyImport = new Guid(0xee62470b, 0xe94b, 0x424e, 0x9b, 0x7c, 0x2f, 0, 0xc9, 0x24, 0x9f, 0x93);
     IID_IMetaDataTables = new Guid(0xd8f579ab, 0x402d, 0x4b8e, 130, 0xd9, 0x5d, 0x63, 0xb1, 6, 0x5c, 0x68);
 }
Esempio n. 36
0
        public static unsafe Object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = 0;
            long           buffer         = 0;
            int            length;
            string         stringVal;

            stringVal = scope.GetDefaultValue(token, out buffer, out length, out corElementType);

            RuntimeType fieldType = fieldTypeHandle.GetRuntimeType();

            if (fieldType.IsEnum && raw == false)
            {
                // NOTE: Unlike in `TypeBuilder.SetConstantValue`, if `fieldType` describes
                // a nullable enum type `Nullable<TEnum>`, we do not unpack it to `TEnum` to
                // successfully enter this `if` clause. Default values of `TEnum?`-typed
                // parameters have been reported as values of the underlying type, changing
                // this now might be a breaking change.

                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    defaultValue = *(char *)&buffer;
                    break;

                case CorElementType.I1:
                    defaultValue = *(sbyte *)&buffer;
                    break;

                case CorElementType.U1:
                    defaultValue = *(byte *)&buffer;
                    break;

                case CorElementType.I2:
                    defaultValue = *(short *)&buffer;
                    break;

                case CorElementType.U2:
                    defaultValue = *(ushort *)&buffer;
                    break;

                case CorElementType.I4:
                    defaultValue = *(int *)&buffer;
                    break;

                case CorElementType.U4:
                    defaultValue = *(uint *)&buffer;
                    break;

                case CorElementType.I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.U8:
                    defaultValue = buffer;
                    break;

                case CorElementType.Class:
                    return(null);

                default:
                    throw new FormatException(SR.Arg_BadLiteralFormat);
                    #endregion
                }

                return(RuntimeType.CreateEnum(fieldType, defaultValue));
            }
            else if (fieldType == typeof(DateTime))
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.U8:
                    defaultValue = buffer;
                    break;

                case CorElementType.Class:
                    return(null);

                default:
                    throw new FormatException(SR.Arg_BadLiteralFormat);
                    #endregion
                }

                return(new DateTime(defaultValue));
            }
            else
            {
                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    return(*(char *)&buffer);

                case CorElementType.I1:
                    return(*(sbyte *)&buffer);

                case CorElementType.U1:
                    return(*(byte *)&buffer);

                case CorElementType.I2:
                    return(*(short *)&buffer);

                case CorElementType.U2:
                    return(*(ushort *)&buffer);

                case CorElementType.I4:
                    return(*(int *)&buffer);

                case CorElementType.U4:
                    return(*(uint *)&buffer);

                case CorElementType.I8:
                    return(buffer);

                case CorElementType.U8:
                    return((ulong)buffer);

                case CorElementType.Boolean:
                    // The boolean value returned from the metadata engine is stored as a
                    // BOOL, which actually maps to an int. We need to read it out as an int
                    // to avoid problems on big-endian machines.
                    return(*(int *)&buffer != 0);

                case CorElementType.R4:
                    return(*(float *)&buffer);

                case CorElementType.R8:
                    return(*(double *)&buffer);

                case CorElementType.String:
                    // A string constant can be empty but never null.
                    // A nullref constant can only be type CorElementType.Class.
                    return(stringVal == null ? string.Empty : stringVal);

                case CorElementType.Class:
                    return(null);

                default:
                    throw new FormatException(SR.Arg_BadLiteralFormat);
                    #endregion
                }
            }
        }
Esempio n. 37
0
 private void OnDeserialized(StreamingContext context)
 {
     ParameterInfo returnParameter = null;
     if (this.MemberImpl == null)
     {
         throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
     }
     ParameterInfo[] indexParameters = null;
     MemberTypes memberType = this.MemberImpl.MemberType;
     if ((memberType != MemberTypes.Constructor) && (memberType != MemberTypes.Method))
     {
         if (memberType != MemberTypes.Property)
         {
             throw new SerializationException(Environment.GetResourceString("Serialization_NoParameterInfo"));
         }
     }
     else
     {
         if (this.PositionImpl != -1)
         {
             indexParameters = ((MethodBase) this.MemberImpl).GetParametersNoCopy();
             if ((indexParameters == null) || (this.PositionImpl >= indexParameters.Length))
             {
                 throw new SerializationException(Environment.GetResourceString("Serialization_BadParameterInfo"));
             }
             returnParameter = indexParameters[this.PositionImpl];
         }
         else
         {
             if (this.MemberImpl.MemberType != MemberTypes.Method)
             {
                 throw new SerializationException(Environment.GetResourceString("Serialization_BadParameterInfo"));
             }
             returnParameter = ((MethodInfo) this.MemberImpl).ReturnParameter;
         }
         goto Label_0104;
     }
     indexParameters = ((PropertyInfo) this.MemberImpl).GetIndexParameters();
     if (((indexParameters == null) || (this.PositionImpl <= -1)) || (this.PositionImpl >= indexParameters.Length))
     {
         throw new SerializationException(Environment.GetResourceString("Serialization_BadParameterInfo"));
     }
     returnParameter = indexParameters[this.PositionImpl];
 Label_0104:
     this.m_tkParamDef = returnParameter.m_tkParamDef;
     this.m_scope = returnParameter.m_scope;
     this.m_signature = returnParameter.m_signature;
     this.m_nameIsCached = true;
 }
Esempio n. 38
0
 internal bool Equals(MetadataImport import)
 {
     return (import.m_metadataImport2 == this.m_metadataImport2);
 }
 public void GetCustomAttributeProps(int customAttributeToken, out int constructorToken, out ConstArray signature)
 {
     MetadataImport._GetCustomAttributeProps(this.m_metadataImport2, customAttributeToken, out constructorToken, out signature);
 }
 private bool Equals(MetadataImport import)
 {
     return import.m_metadataImport2 == m_metadataImport2;
 }
Esempio n. 41
0
        [System.Security.SecurityCritical]  // auto-generated
        public static unsafe Object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = 0;
            long buffer = 0;
            int length;
            String stringVal;

            stringVal = scope.GetDefaultValue(token, out buffer, out length, out corElementType);

            RuntimeType fieldType = fieldTypeHandle.GetRuntimeType();

            if (fieldType.IsEnum && raw == false)
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                    case CorElementType.Void:
                        return DBNull.Value;

                    case CorElementType.Char:
                        defaultValue = *(char*)&buffer;
                        break;

                    case CorElementType.I1:
                        defaultValue = *(sbyte*)&buffer;
                        break;

                    case CorElementType.U1:
                        defaultValue = *(byte*)&buffer;
                        break;

                    case CorElementType.I2:
                        defaultValue = *(short*)&buffer;
                        break;

                    case CorElementType.U2:
                        defaultValue = *(ushort*)&buffer;
                        break;

                    case CorElementType.I4:
                        defaultValue = *(int*)&buffer;
                        break;

                    case CorElementType.U4:
                        defaultValue = *(uint*)&buffer;
                        break;

                    case CorElementType.I8:
                        defaultValue = buffer;
                        break;

                    case CorElementType.U8:
                        defaultValue = buffer;
                        break;
                
                    default:
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }

                return RuntimeType.CreateEnum(fieldType, defaultValue);                       
            }
            else if (fieldType == typeof(DateTime))
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                    case CorElementType.Void:
                        return DBNull.Value;
                        
                    case CorElementType.I8:
                        defaultValue = buffer;
                        break;

                    case CorElementType.U8:
                        defaultValue = buffer;
                        break;
                
                    default:
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion  
                }

                return new DateTime(defaultValue);
            }
            else
            {
                switch (corElementType)
                {
                    #region Switch

                    case CorElementType.Void:
                        return DBNull.Value;

                    case CorElementType.Char:
                        return *(char*)&buffer;

                    case CorElementType.I1:
                        return *(sbyte*)&buffer;

                    case CorElementType.U1:
                        return *(byte*)&buffer;

                    case CorElementType.I2:
                        return *(short*)&buffer;

                    case CorElementType.U2:
                        return *(ushort*)&buffer;

                    case CorElementType.I4:
                        return *(int*)&buffer;

                    case CorElementType.U4:
                        return *(uint*)&buffer;

                    case CorElementType.I8:
                        return buffer;

                    case CorElementType.U8:
                        return (ulong)buffer;
                
                    case CorElementType.Boolean :
                        // The boolean value returned from the metadata engine is stored as a
                        // BOOL, which actually maps to an int. We need to read it out as an int
                        // to avoid problems on big-endian machines.
                        return (*(int*)&buffer != 0);

                    case CorElementType.R4 :
                        return *(float*)&buffer;

                    case CorElementType.R8:
                        return *(double*)&buffer;

                    case CorElementType.String:
                        // A string constant can be empty but never null.
                        // A nullref constant can only be type CorElementType.Class.
                        return stringVal == null ? String.Empty : stringVal;

                    case CorElementType.Class:
                        return null;
                    
                    default:
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }
            }
        } 
Esempio n. 42
0
        internal static unsafe ParameterInfo[] GetParameters(IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo returnParameter, bool fetchReturnParameter)
        {
            returnParameter = null;
            int length = sig.Arguments.Length;

            ParameterInfo[] infoArray = fetchReturnParameter ? null : new ParameterInfo[length];
            int             methodDef = RuntimeMethodHandle.GetMethodDef(methodHandle);
            int             count     = 0;

            if (!System.Reflection.MetadataToken.IsNullToken(methodDef))
            {
                MetadataImport metadataImport = RuntimeTypeHandle.GetMetadataImport(RuntimeMethodHandle.GetDeclaringType(methodHandle));
                count = metadataImport.EnumParamsCount(methodDef);
                int *result = (int *)stackalloc byte[(((IntPtr)count) * 4)];
                metadataImport.EnumParams(methodDef, result, count);
                if (count > (length + 1))
                {
                    throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                }
                for (uint i = 0; i < count; i++)
                {
                    ParameterAttributes attributes;
                    int num5;
                    int parameterToken = result[(int)((int *)i)];
                    metadataImport.GetParamDefProps(parameterToken, out num5, out attributes);
                    num5--;
                    if (fetchReturnParameter && (num5 == -1))
                    {
                        if (returnParameter != null)
                        {
                            throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                        }
                        returnParameter = new RuntimeParameterInfo(sig, metadataImport, parameterToken, num5, attributes, member);
                    }
                    else if (!fetchReturnParameter && (num5 >= 0))
                    {
                        if (num5 >= length)
                        {
                            throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                        }
                        infoArray[num5] = new RuntimeParameterInfo(sig, metadataImport, parameterToken, num5, attributes, member);
                    }
                }
            }
            if (fetchReturnParameter)
            {
                if (returnParameter == null)
                {
                    returnParameter = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, -1, ParameterAttributes.None, member);
                }
                return(infoArray);
            }
            if (count < (infoArray.Length + 1))
            {
                for (int j = 0; j < infoArray.Length; j++)
                {
                    if (infoArray[j] == null)
                    {
                        infoArray[j] = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, j, ParameterAttributes.None, member);
                    }
                }
            }
            return(infoArray);
        }
Esempio n. 43
0
        internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo returnParameter, bool fetchReturnParameter)
        {
            returnParameter = (ParameterInfo)null;
            int length = sig.Arguments.Length;

            ParameterInfo[] parameterInfoArray = fetchReturnParameter ? (ParameterInfo[])null : new ParameterInfo[length];
            int             methodDef          = RuntimeMethodHandle.GetMethodDef(methodHandle);
            int             num1 = 0;

            if (!System.Reflection.MetadataToken.IsNullToken(methodDef))
            {
                MetadataImport     metadataImport = RuntimeTypeHandle.GetMetadataImport(RuntimeMethodHandle.GetDeclaringType(methodHandle));
                MetadataEnumResult result;
                metadataImport.EnumParams(methodDef, out result);
                num1 = result.Length;
                if (num1 > length + 1)
                {
                    throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                }
                for (int index = 0; index < num1; ++index)
                {
                    int num2 = result[index];
                    int sequence;
                    ParameterAttributes attributes;
                    metadataImport.GetParamDefProps(num2, out sequence, out attributes);
                    --sequence;
                    if (fetchReturnParameter && sequence == -1)
                    {
                        if (returnParameter != null)
                        {
                            throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                        }
                        returnParameter = (ParameterInfo) new RuntimeParameterInfo(sig, metadataImport, num2, sequence, attributes, member);
                    }
                    else if (!fetchReturnParameter && sequence >= 0)
                    {
                        if (sequence >= length)
                        {
                            throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                        }
                        parameterInfoArray[sequence] = (ParameterInfo) new RuntimeParameterInfo(sig, metadataImport, num2, sequence, attributes, member);
                    }
                }
            }
            if (fetchReturnParameter)
            {
                if (returnParameter == null)
                {
                    returnParameter = (ParameterInfo) new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, -1, ParameterAttributes.None, member);
                }
            }
            else if (num1 < parameterInfoArray.Length + 1)
            {
                for (int position = 0; position < parameterInfoArray.Length; ++position)
                {
                    if (parameterInfoArray[position] == null)
                    {
                        parameterInfoArray[position] = (ParameterInfo) new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, position, ParameterAttributes.None, member);
                    }
                }
            }
            return(parameterInfoArray);
        }
        internal unsafe static bool FilterCustomAttributeRecord(
            CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, 
            Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable,
            object[] attributes, IList derivedAttributes,
            out RuntimeType attributeType, out RuntimeMethodHandle ctor, out bool ctorHasParameters, out bool isVarArg)
        {
            ctor = new RuntimeMethodHandle();
            attributeType = null;
            ctorHasParameters = false;
            isVarArg = false;
            
            IntPtr blobStart = caRecord.blob.Signature;
            IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length);

            // Resolve attribute type from ctor parent token found in decorated decoratedModule scope
            attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType;

            // Test attribute type against user provided attribute type filter
            if (!(attributeFilterType.IsAssignableFrom(attributeType)))
                return false;

            if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
                return false;

            // APTCA checks
            if (attributeType.Assembly != lastAptcaOkAssembly && 
                !attributeType.Assembly.AptcaCheck(decoratedModule.Assembly))
                return false;

            // Cache last successful APTCA check (optimization)
            lastAptcaOkAssembly = decoratedModule.Assembly;

            // Resolve the attribute ctor
            ConstArray ctorSig = scope.GetMethodSignature(caRecord.tkCtor);
            isVarArg = (ctorSig[0] & 0x05) != 0;
            ctorHasParameters = ctorSig[1] != 0;

            if (ctorHasParameters)
            {
                // Resolve method ctor token found in decorated decoratedModule scope
                ctor = decoratedModule.ModuleHandle.ResolveMethodHandle(caRecord.tkCtor);
            }
            else
            {
                // Resolve method ctor token from decorated decoratedModule scope
                ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();

                if (ctor.IsNullHandle() && !attributeType.IsValueType)
                    throw new MissingMethodException(".ctor");
            }

            // Visibility checks
            if (ctor.IsNullHandle())
            {
                if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule.ModuleHandle))
                    return false;            
                
                return true;
            }
            
            if (ctor.IsVisibleFromModule(decoratedModule))
                return true;
            
            MetadataToken tkParent = new MetadataToken();
                
            if (decoratedToken.IsParamDef)
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));
                tkParent = new MetadataToken(scope.GetParentToken(tkParent));
            }               
            else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) 
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));
            }
            else if (decoratedToken.IsTypeDef)
            {
                tkParent = decoratedToken;
            }
            
            if (tkParent.IsTypeDef)
                return ctor.IsVisibleFromType(decoratedModule.ModuleHandle.ResolveTypeHandle(tkParent));
            
            return false;
        }
        internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo returnParameter, bool fetchReturnParameter)
        {
            returnParameter = null;
            int num = sig.Arguments.Length;

            ParameterInfo[] array     = fetchReturnParameter ? null : new ParameterInfo[num];
            int             methodDef = RuntimeMethodHandle.GetMethodDef(methodHandle);
            int             num2      = 0;

            if (!System.Reflection.MetadataToken.IsNullToken(methodDef))
            {
                MetadataImport     metadataImport = RuntimeTypeHandle.GetMetadataImport(RuntimeMethodHandle.GetDeclaringType(methodHandle));
                MetadataEnumResult metadataEnumResult;
                metadataImport.EnumParams(methodDef, out metadataEnumResult);
                num2 = metadataEnumResult.Length;
                if (num2 > num + 1)
                {
                    throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                }
                for (int i = 0; i < num2; i++)
                {
                    int num3 = metadataEnumResult[i];
                    int num4;
                    ParameterAttributes attributes;
                    metadataImport.GetParamDefProps(num3, out num4, out attributes);
                    num4--;
                    if (fetchReturnParameter && num4 == -1)
                    {
                        if (returnParameter != null)
                        {
                            throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                        }
                        returnParameter = new RuntimeParameterInfo(sig, metadataImport, num3, num4, attributes, member);
                    }
                    else if (!fetchReturnParameter && num4 >= 0)
                    {
                        if (num4 >= num)
                        {
                            throw new BadImageFormatException(Environment.GetResourceString("BadImageFormat_ParameterSignatureMismatch"));
                        }
                        array[num4] = new RuntimeParameterInfo(sig, metadataImport, num3, num4, attributes, member);
                    }
                }
            }
            if (fetchReturnParameter)
            {
                if (returnParameter == null)
                {
                    returnParameter = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, -1, ParameterAttributes.None, member);
                }
            }
            else if (num2 < array.Length + 1)
            {
                for (int j = 0; j < array.Length; j++)
                {
                    if (array[j] == null)
                    {
                        array[j] = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, j, ParameterAttributes.None, member);
                    }
                }
            }
            return(array);
        }
Esempio n. 46
0
        [System.Security.SecurityCritical]  // auto-generated
        private unsafe static bool FilterCustomAttributeRecord(
            CustomAttributeRecord caRecord,
            MetadataImport scope,
            ref Assembly lastAptcaOkAssembly, 
            RuntimeModule decoratedModule,
            MetadataToken decoratedToken,
            RuntimeType attributeFilterType,
            bool mustBeInheritable,
            object[] attributes,
            IList derivedAttributes,
            out RuntimeType attributeType,
            out IRuntimeMethodInfo ctor,
            out bool ctorHasParameters,
            out bool isVarArg)
        {
            ctor = null;
            attributeType = null;
            ctorHasParameters = false;
            isVarArg = false;
            
            IntPtr blobStart = caRecord.blob.Signature;
            IntPtr blobEnd = (IntPtr)((byte*)blobStart + caRecord.blob.Length);

#if FEATURE_LEGACYNETCF
            if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) {
                try
                {
                    // Resolve attribute type from ctor parent token found in decorated decoratedModule scope
                    attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType;
                }
                catch(Exception)
                {
                    return false;
                }
            }
            else
#endif
            // Resolve attribute type from ctor parent token found in decorated decoratedModule scope
            attributeType = decoratedModule.ResolveType(scope.GetParentToken(caRecord.tkCtor), null, null) as RuntimeType;


            // Test attribute type against user provided attribute type filter
            if (!(attributeFilterType.IsAssignableFrom(attributeType)))
                return false;

            // Ensure if attribute type must be inheritable that it is inhertiable
            // Ensure that to consider a duplicate attribute type AllowMultiple is true
            if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes))
                return false;

            // Windows Runtime attributes aren't real types - they exist to be read as metadata only, and as such
            // should be filtered out of the GetCustomAttributes path.
            if ((attributeType.Attributes & TypeAttributes.WindowsRuntime) == TypeAttributes.WindowsRuntime)
            {
                return false;
            }

#if FEATURE_APTCA
            // APTCA checks
            RuntimeAssembly attributeAssembly = (RuntimeAssembly)attributeType.Assembly;
            RuntimeAssembly decoratedModuleAssembly = (RuntimeAssembly)decoratedModule.Assembly;

            if (attributeAssembly != lastAptcaOkAssembly && 
                !RuntimeAssembly.AptcaCheck(attributeAssembly, decoratedModuleAssembly))
                return false;

            // Cache last successful APTCA check (optimization)
            lastAptcaOkAssembly = decoratedModuleAssembly;
#endif // FEATURE_APTCA

            // Resolve the attribute ctor
            ConstArray ctorSig = scope.GetMethodSignature(caRecord.tkCtor);
            isVarArg = (ctorSig[0] & 0x05) != 0;
            ctorHasParameters = ctorSig[1] != 0;

            if (ctorHasParameters)
            {
                // Resolve method ctor token found in decorated decoratedModule scope
                ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor);
            }
            else
            {
                // Resolve method ctor token from decorated decoratedModule scope
                ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor();

                if (ctor == null && !attributeType.IsValueType)
                    throw new MissingMethodException(".ctor");
            }

            // Visibility checks
            MetadataToken tkParent = new MetadataToken();
                
            if (decoratedToken.IsParamDef)
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));
                tkParent = new MetadataToken(scope.GetParentToken(tkParent));
            }               
            else if (decoratedToken.IsMethodDef || decoratedToken.IsProperty || decoratedToken.IsEvent || decoratedToken.IsFieldDef) 
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));
            }
            else if (decoratedToken.IsTypeDef)
            {
                tkParent = decoratedToken;
            }
            else if (decoratedToken.IsGenericPar)
            {
                tkParent = new MetadataToken(scope.GetParentToken(decoratedToken));

                // decoratedToken is a generic parameter on a method. Get the declaring Type of the method.
                if (tkParent.IsMethodDef)
                    tkParent = new MetadataToken(scope.GetParentToken(tkParent));
            }
            else
            {
                // We need to relax this when we add support for other types of decorated tokens.
                Contract.Assert(decoratedToken.IsModule || decoratedToken.IsAssembly, 
                                "The decoratedToken must be either an assembly, a module, a type, or a member.");
            }

            // If the attribute is on a type, member, or parameter we check access against the (declaring) type,
            // otherwise we check access against the module.
            RuntimeTypeHandle parentTypeHandle = tkParent.IsTypeDef ?
                                                    decoratedModule.ModuleHandle.ResolveTypeHandle(tkParent) :
                                                    new RuntimeTypeHandle();

            return RuntimeMethodHandle.IsCAVisibleFromDecoratedType(attributeType.TypeHandle, ctor, parentTypeHandle, decoratedModule);
        }
Esempio n. 47
0
        public static unsafe object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = CorElementType.End;
            long           num1           = 0;
            int            length;
            string         defaultValue = scope.GetDefaultValue(token, out num1, out length, out corElementType);
            RuntimeType    runtimeType  = fieldTypeHandle.GetRuntimeType();

            if (runtimeType.IsEnum && !raw)
            {
                long num2;
                switch (corElementType)
                {
                case CorElementType.Void:
                    return((object)DBNull.Value);

                case CorElementType.Char:
                    num2 = (long)*(ushort *)&num1;
                    break;

                case CorElementType.I1:
                    num2 = (long)*(sbyte *)&num1;
                    break;

                case CorElementType.U1:
                    num2 = (long)*(byte *)&num1;
                    break;

                case CorElementType.I2:
                    num2 = (long)*(short *)&num1;
                    break;

                case CorElementType.U2:
                    num2 = (long)*(ushort *)&num1;
                    break;

                case CorElementType.I4:
                    num2 = (long)*(int *)&num1;
                    break;

                case CorElementType.U4:
                    num2 = (long)*(uint *)&num1;
                    break;

                case CorElementType.I8:
                    num2 = num1;
                    break;

                case CorElementType.U8:
                    num2 = num1;
                    break;

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                }
                return(RuntimeType.CreateEnum(runtimeType, num2));
            }
            if ((Type)runtimeType == typeof(DateTime))
            {
                if (corElementType == CorElementType.Void)
                {
                    return((object)DBNull.Value);
                }
                long ticks;
                if (corElementType != CorElementType.I8)
                {
                    if (corElementType != CorElementType.U8)
                    {
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    }
                    ticks = num1;
                }
                else
                {
                    ticks = num1;
                }
                return((object)new DateTime(ticks));
            }
            switch (corElementType)
            {
            case CorElementType.Void:
                return((object)DBNull.Value);

            case CorElementType.Boolean:
                return((object)((uint)*(int *)&num1 > 0U));

            case CorElementType.Char:
                return((object)(char)*(ushort *)&num1);

            case CorElementType.I1:
                return((object)*(sbyte *)&num1);

            case CorElementType.U1:
                return((object)*(byte *)&num1);

            case CorElementType.I2:
                return((object)*(short *)&num1);

            case CorElementType.U2:
                return((object)*(ushort *)&num1);

            case CorElementType.I4:
                return((object)*(int *)&num1);

            case CorElementType.U4:
                return((object)*(uint *)&num1);

            case CorElementType.I8:
                return((object)num1);

            case CorElementType.U8:
                return((object)(ulong)num1);

            case CorElementType.R4:
                return((object)*(float *)&num1);

            case CorElementType.R8:
                return((object)*(double *)&num1);

            case CorElementType.String:
                return((object)defaultValue ?? (object)string.Empty);

            case CorElementType.Class:
                return((object)null);

            default:
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
            }
        }
Esempio n. 48
0
        private RuntimeParameterInfo(
            Signature signature, MetadataImport scope, int tkParamDef,
            int position, ParameterAttributes attributes, MemberInfo member)
        {
            Contract.Requires(member != null);
            Contract.Assert(MdToken.IsNullToken(tkParamDef) == scope.Equals(MetadataImport.EmptyImport));
            Contract.Assert(MdToken.IsNullToken(tkParamDef) || MdToken.IsTokenOfType(tkParamDef, MetadataTokenType.ParamDef));

            PositionImpl = position;
            MemberImpl = member;
            m_signature = signature;
            m_tkParamDef = MdToken.IsNullToken(tkParamDef) ? (int)MetadataTokenType.ParamDef : tkParamDef;
            m_scope = scope;
            AttrsImpl = attributes;

            ClassImpl = null;
            NameImpl = null;
        }
        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"));
            }
            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(CustomAttribute.CreateAttributeArrayHelper(elementType, 0));
            }
            object[]             array = CustomAttribute.CreateAttributeArrayHelper(elementType, customAttributeRecords.Length);
            int                  num   = 0;
            SecurityContextFrame securityContextFrame = default(SecurityContextFrame);

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

            for (int i = 0; i < customAttributeRecords.Length; i++)
            {
                object obj = null;
                CustomAttributeRecord caRecord          = customAttributeRecords[i];
                IRuntimeMethodInfo    runtimeMethodInfo = null;
                RuntimeType           runtimeType       = null;
                int    num2    = 0;
                IntPtr intPtr  = caRecord.blob.Signature;
                IntPtr intPtr2 = (IntPtr)((void *)((byte *)((void *)intPtr) + caRecord.blob.Length));
                int    num3    = (int)((long)((byte *)((void *)intPtr2) - (byte *)((void *)intPtr)));
                bool   flag;
                bool   isVarArg;
                if (CustomAttribute.FilterCustomAttributeRecord(caRecord, metadataImport, ref assembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, array, derivedAttributes, out runtimeType, out runtimeMethodInfo, out flag, out isVarArg))
                {
                    if (runtimeMethodInfo != null)
                    {
                        RuntimeMethodHandle.CheckLinktimeDemands(runtimeMethodInfo, decoratedModule, isDecoratedTargetSecurityTransparent);
                    }
                    RuntimeConstructorInfo.CheckCanCreateInstance(runtimeType, isVarArg);
                    if (flag)
                    {
                        obj = CustomAttribute.CreateCaObject(decoratedModule, runtimeMethodInfo, ref intPtr, intPtr2, out num2);
                    }
                    else
                    {
                        obj = RuntimeTypeHandle.CreateCaInstance(runtimeType, runtimeMethodInfo);
                        if (num3 == 0)
                        {
                            num2 = 0;
                        }
                        else
                        {
                            if (Marshal.ReadInt16(intPtr) != 1)
                            {
                                throw new CustomAttributeFormatException();
                            }
                            intPtr = (IntPtr)((void *)((byte *)((void *)intPtr) + 2));
                            num2   = (int)Marshal.ReadInt16(intPtr);
                            intPtr = (IntPtr)((void *)((byte *)((void *)intPtr) + 2));
                        }
                    }
                    for (int j = 0; j < num2; j++)
                    {
                        IntPtr      signature = caRecord.blob.Signature;
                        string      text;
                        bool        flag2;
                        RuntimeType runtimeType2;
                        object      obj2;
                        CustomAttribute.GetPropertyOrFieldData(decoratedModule, ref intPtr, intPtr2, out text, out flag2, out runtimeType2, out obj2);
                        try
                        {
                            if (flag2)
                            {
                                if (runtimeType2 == null && obj2 != null)
                                {
                                    runtimeType2 = (RuntimeType)obj2.GetType();
                                    if (runtimeType2 == CustomAttribute.Type_RuntimeType)
                                    {
                                        runtimeType2 = CustomAttribute.Type_Type;
                                    }
                                }
                                RuntimePropertyInfo runtimePropertyInfo;
                                if (runtimeType2 == null)
                                {
                                    runtimePropertyInfo = (runtimeType.GetProperty(text) as RuntimePropertyInfo);
                                }
                                else
                                {
                                    runtimePropertyInfo = (runtimeType.GetProperty(text, runtimeType2, Type.EmptyTypes) as RuntimePropertyInfo);
                                }
                                if (runtimePropertyInfo == null)
                                {
                                    throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag2 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), text));
                                }
                                RuntimeMethodInfo runtimeMethodInfo2 = runtimePropertyInfo.GetSetMethod(true) as RuntimeMethodInfo;
                                if (runtimeMethodInfo2.IsPublic)
                                {
                                    RuntimeMethodHandle.CheckLinktimeDemands(runtimeMethodInfo2, decoratedModule, isDecoratedTargetSecurityTransparent);
                                    runtimeMethodInfo2.UnsafeInvoke(obj, BindingFlags.Default, null, new object[]
                                    {
                                        obj2
                                    }, null);
                                }
                            }
                            else
                            {
                                RtFieldInfo rtFieldInfo = runtimeType.GetField(text) as RtFieldInfo;
                                if (isDecoratedTargetSecurityTransparent)
                                {
                                    RuntimeFieldHandle.CheckAttributeAccess(rtFieldInfo.FieldHandle, decoratedModule.GetNativeHandle());
                                }
                                rtFieldInfo.CheckConsistency(obj);
                                rtFieldInfo.UnsafeSetValue(obj, obj2, BindingFlags.Default, Type.DefaultBinder, null);
                            }
                        }
                        catch (Exception inner)
                        {
                            throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag2 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), text), inner);
                        }
                    }
                    if (!intPtr.Equals(intPtr2))
                    {
                        throw new CustomAttributeFormatException();
                    }
                    array[num++] = obj;
                }
            }
            securityContextFrame.Pop();
            if (num == customAttributeRecords.Length && pcaCount == 0)
            {
                return(array);
            }
            object[] array2 = CustomAttribute.CreateAttributeArrayHelper(elementType, num + pcaCount);
            Array.Copy(array, 0, array2, 0, num);
            return(array2);
        }
Esempio n. 50
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static unsafe void AssignAssociates(
            MetadataImport scope,
            int mdPropEvent,
            RuntimeType declaringType,
            RuntimeType reflectedType,
            out RuntimeMethodInfo addOn,
            out RuntimeMethodInfo removeOn,
            out RuntimeMethodInfo fireOn,
            out RuntimeMethodInfo getter,
            out RuntimeMethodInfo setter,
            out MethodInfo[] other,
            out bool composedOfAllPrivateMethods,
            out BindingFlags bindingFlags)
        {
            addOn = removeOn = fireOn = getter = setter = null;

            Attributes attributes = 
                Attributes.ComposedOfAllPrivateMethods |
                Attributes.ComposedOfAllVirtualMethods |
                Attributes.ComposedOfNoPublicMembers |
                Attributes.ComposedOfNoStaticMembers;

            while(RuntimeTypeHandle.IsGenericVariable(reflectedType))
                reflectedType = (RuntimeType)reflectedType.BaseType;

            bool isInherited = declaringType != reflectedType;

            List<MethodInfo> otherList = null;

            MetadataEnumResult associatesData;
            scope.Enum(MetadataTokenType.MethodDef, mdPropEvent, out associatesData);

            int cAssociates = associatesData.Length / 2;

            for (int i = 0; i < cAssociates; i++)
            {
                int methodDefToken = associatesData[i * 2];
                MethodSemanticsAttributes semantics = (MethodSemanticsAttributes)associatesData[i * 2 + 1];

                #region Assign each associate
                RuntimeMethodInfo associateMethod =
                    AssignAssociates(methodDefToken, declaringType, reflectedType);

                if (associateMethod == null)
                    continue;

                MethodAttributes methAttr = associateMethod.Attributes;
                bool isPrivate =(methAttr & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
                bool isVirtual =(methAttr & MethodAttributes.Virtual) != 0;

                MethodAttributes visibility = methAttr & MethodAttributes.MemberAccessMask;
                bool isPublic = visibility == MethodAttributes.Public;
                bool isStatic =(methAttr & MethodAttributes.Static) != 0;

                if (isPublic)
                {
                    attributes &= ~Attributes.ComposedOfNoPublicMembers;
                    attributes &= ~Attributes.ComposedOfAllPrivateMethods;
                }
                else if (!isPrivate)
                {
                    attributes &= ~Attributes.ComposedOfAllPrivateMethods;
                }

                if (isStatic)
                    attributes &= ~Attributes.ComposedOfNoStaticMembers;

                if (!isVirtual)
                    attributes &= ~Attributes.ComposedOfAllVirtualMethods;
                #endregion

                if (semantics == MethodSemanticsAttributes.Setter)
                    setter = associateMethod;
                else if (semantics == MethodSemanticsAttributes.Getter)
                    getter = associateMethod;
                else if (semantics == MethodSemanticsAttributes.Fire)
                    fireOn = associateMethod;
                else if (semantics == MethodSemanticsAttributes.AddOn)
                    addOn = associateMethod;
                else if (semantics == MethodSemanticsAttributes.RemoveOn)
                    removeOn = associateMethod;
                else
                {
                    if (otherList == null)
                        otherList = new List<MethodInfo>(cAssociates);
                    otherList.Add(associateMethod);
                }
            }

            bool isPseudoPublic = (attributes & Attributes.ComposedOfNoPublicMembers) == 0;
            bool isPseudoStatic = (attributes & Attributes.ComposedOfNoStaticMembers) == 0;
            bindingFlags = RuntimeType.FilterPreCalculate(isPseudoPublic, isInherited, isPseudoStatic);

            composedOfAllPrivateMethods =(attributes & Attributes.ComposedOfAllPrivateMethods) != 0;

            other = (otherList != null) ? otherList.ToArray() : null;
        }
Esempio n. 51
0
        public override MethodBase?ResolveMethod(
            int metadataToken,
            Type[]?genericTypeArguments,
            Type[]?genericMethodArguments
            )
        {
            try
            {
                MetadataToken tk = new MetadataToken(metadataToken);
                if (!tk.IsMethodDef && !tk.IsMethodSpec)
                {
                    if (!tk.IsMemberRef)
                    {
                        throw new ArgumentException(
                                  SR.Format(SR.Argument_ResolveMethod, tk, this),
                                  nameof(metadataToken)
                                  );
                    }
                    unsafe
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);

                        if (*(MdSigCallingConvention *)sig.Signature == MdSigCallingConvention.Field)
                        {
                            throw new ArgumentException(
                                      SR.Format(SR.Argument_ResolveMethod, tk, this),
                                      nameof(metadataToken)
                                      );
                        }
                    }
                }

                RuntimeTypeHandle[]? typeArgs   = null;
                RuntimeTypeHandle[]? methodArgs = null;
                if (genericTypeArguments?.Length > 0)
                {
                    typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
                }
                if (genericMethodArguments?.Length > 0)
                {
                    methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
                }

                ModuleHandle       moduleHandle = new ModuleHandle(this);
                IRuntimeMethodInfo methodHandle = moduleHandle
                                                  .ResolveMethodHandle(tk, typeArgs, methodArgs)
                                                  .GetMethodInfo();

                Type declaringType = RuntimeMethodHandle.GetDeclaringType(methodHandle);

                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    MetadataToken tkDeclaringType = new MetadataToken(
                        MetadataImport.GetParentToken(tk)
                        );

                    if (tk.IsMethodSpec)
                    {
                        tkDeclaringType = new MetadataToken(
                            MetadataImport.GetParentToken(tkDeclaringType)
                            );
                    }

                    declaringType = ResolveType(
                        tkDeclaringType,
                        genericTypeArguments,
                        genericMethodArguments
                        );
                }

                return(RuntimeType.GetMethodBase(declaringType as RuntimeType, methodHandle));
            }
            catch (BadImageFormatException e)
            {
                throw new ArgumentException(SR.Argument_BadImageFormatExceptionResolve, e);
            }
        }