Ejemplo n.º 1
0
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
            }

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage))
            {
                FrameworkEventSource.Log.BeginGetFieldFromHandle();
            }
#endif

            FieldInfo f = RuntimeType.GetFieldInfo(declaringType.GetRuntimeType(), handle.GetRuntimeFieldInfo());

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage) && declaringType != null && f != null)
            {
                FrameworkEventSource.Log.EndGetFieldFromHandle(declaringType.GetRuntimeType().GetFullNameForEtw(), f.GetFullNameForEtw());
            }
#endif

            return(f);
        }
Ejemplo n.º 2
0
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
            }

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage))
            {
                FrameworkEventSource.Log.BeginGetFieldFromHandle();
            }
#endif

            FieldInfo f = RuntimeType.GetFieldInfo(handle.GetRuntimeFieldInfo());

            Type declaringType = f.DeclaringType;

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage) && declaringType != null && f != null)
            {
                FrameworkEventSource.Log.EndGetFieldFromHandle(declaringType.GetFullNameForEtw(), f.GetFullNameForEtw());
            }
#endif

            if (declaringType != null && declaringType.IsGenericType)
            {
                throw new ArgumentException(String.Format(
                                                CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_FieldDeclaringTypeGeneric"),
                                                f.Name, declaringType.GetGenericTypeDefinition()));
            }

            return(f);
        }
Ejemplo n.º 3
0
 public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
 {
     if (handle.IsNullHandle())
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
     }
     return(RuntimeType.GetFieldInfo(declaringType, handle));
 }
Ejemplo n.º 4
0
        public unsafe override FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken metadataToken2 = new MetadataToken(metadataToken);

            if (!this.MetadataImport.IsValidToken(metadataToken2))
            {
                throw new ArgumentOutOfRangeException("metadataToken", Environment.GetResourceString("Argument_InvalidToken", new object[]
                {
                    metadataToken2,
                    this
                }));
            }
            RuntimeTypeHandle[] typeInstantiationContext   = RuntimeModule.ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodInstantiationContext = RuntimeModule.ConvertToTypeHandleArray(genericMethodArguments);
            FieldInfo           result;

            try
            {
                IRuntimeFieldInfo runtimeFieldInfo;
                if (!metadataToken2.IsFieldDef)
                {
                    if (!metadataToken2.IsMemberRef)
                    {
                        throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", new object[]
                        {
                            metadataToken2,
                            this
                        }));
                    }
                    if (*(byte *)this.MetadataImport.GetMemberRefProps(metadataToken2).Signature.ToPointer() != 6)
                    {
                        throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", new object[]
                        {
                            metadataToken2,
                            this
                        }));
                    }
                    runtimeFieldInfo = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), metadataToken2, typeInstantiationContext, methodInstantiationContext);
                }
                runtimeFieldInfo = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), metadataToken, typeInstantiationContext, methodInstantiationContext);
                RuntimeType runtimeType = RuntimeFieldHandle.GetApproxDeclaringType(runtimeFieldInfo.Value);
                if (runtimeType.IsGenericType || runtimeType.IsArray)
                {
                    int parentToken = ModuleHandle.GetMetadataImport(this.GetNativeHandle()).GetParentToken(metadataToken);
                    runtimeType = (RuntimeType)this.ResolveType(parentToken, genericTypeArguments, genericMethodArguments);
                }
                result = RuntimeType.GetFieldInfo(runtimeType, runtimeFieldInfo);
            }
            catch (MissingFieldException)
            {
                result = this.ResolveLiteralField(metadataToken2, genericTypeArguments, genericMethodArguments);
            }
            catch (BadImageFormatException innerException)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), innerException);
            }
            return(result);
        }
Ejemplo n.º 5
0
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(SR.Argument_InvalidHandle);
            }

            return(RuntimeType.GetFieldInfo(declaringType.GetRuntimeType(), handle.GetRuntimeFieldInfo()));
        }
Ejemplo n.º 6
0
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
            }
            FieldInfo fieldInfo = RuntimeType.GetFieldInfo(handle);

            if ((fieldInfo.DeclaringType != null) && fieldInfo.DeclaringType.IsGenericType)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_FieldDeclaringTypeGeneric"), new object[] { fieldInfo.Name, fieldInfo.DeclaringType.GetGenericTypeDefinition() }));
            }
            return(fieldInfo);
        }
Ejemplo n.º 7
0
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
            }
            FieldInfo fieldInfo     = RuntimeType.GetFieldInfo(handle.GetRuntimeFieldInfo());
            Type      declaringType = fieldInfo.DeclaringType;

            if (declaringType != (Type)null && declaringType.IsGenericType)
            {
                throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_FieldDeclaringTypeGeneric"), (object)fieldInfo.Name, (object)declaringType.GetGenericTypeDefinition()));
            }
            return(fieldInfo);
        }
Ejemplo n.º 8
0
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(SR.Argument_InvalidHandle, nameof(handle));
            }

            FieldInfo f = RuntimeType.GetFieldInfo(handle.GetRuntimeFieldInfo());

            Type declaringType = f.DeclaringType;

            if (declaringType != null && declaringType.IsGenericType)
            {
                throw new ArgumentException(SR.Format(
                                                SR.Argument_FieldDeclaringTypeGeneric,
                                                f.Name, declaringType.GetGenericTypeDefinition()));
            }

            return(f);
        }
Ejemplo n.º 9
0
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"), nameof(handle));
            }

            FieldInfo f = RuntimeType.GetFieldInfo(handle.GetRuntimeFieldInfo());

            Type declaringType = f.DeclaringType;

            if (declaringType != null && declaringType.IsGenericType)
            {
                throw new ArgumentException(String.Format(
                                                CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_FieldDeclaringTypeGeneric"),
                                                f.Name, declaringType.GetGenericTypeDefinition()));
            }

            return(f);
        }
Ejemplo n.º 10
0
 public override unsafe FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
 {
     System.Reflection.MetadataToken metadataToken1 = new System.Reflection.MetadataToken(metadataToken);
     if (!this.MetadataImport.IsValidToken((int)metadataToken1))
     {
         throw new ArgumentOutOfRangeException("metadataToken", Environment.GetResourceString("Argument_InvalidToken", (object)metadataToken1, (object)this));
     }
     RuntimeTypeHandle[] typeHandleArray1 = RuntimeModule.ConvertToTypeHandleArray(genericTypeArguments);
     RuntimeTypeHandle[] typeHandleArray2 = RuntimeModule.ConvertToTypeHandleArray(genericMethodArguments);
     try
     {
         IRuntimeFieldInfo runtimeFieldInfo = (IRuntimeFieldInfo)null;
         if (!metadataToken1.IsFieldDef)
         {
             if (!metadataToken1.IsMemberRef)
             {
                 throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", (object)metadataToken1, (object)this));
             }
             if ((int)*(byte *)this.MetadataImport.GetMemberRefProps((int)metadataToken1).Signature.ToPointer() != 6)
             {
                 throw new ArgumentException("metadataToken", Environment.GetResourceString("Argument_ResolveField", (object)metadataToken1, (object)this));
             }
             runtimeFieldInfo = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), (int)metadataToken1, typeHandleArray1, typeHandleArray2);
         }
         IRuntimeFieldInfo field         = ModuleHandle.ResolveFieldHandleInternal(this.GetNativeHandle(), metadataToken, typeHandleArray1, typeHandleArray2);
         RuntimeType       reflectedType = RuntimeFieldHandle.GetApproxDeclaringType(field.Value);
         if (reflectedType.IsGenericType || reflectedType.IsArray)
         {
             reflectedType = (RuntimeType)this.ResolveType(ModuleHandle.GetMetadataImport(this.GetNativeHandle()).GetParentToken(metadataToken), genericTypeArguments, genericMethodArguments);
         }
         return(RuntimeType.GetFieldInfo(reflectedType, field));
     }
     catch (MissingFieldException ex)
     {
         return(this.ResolveLiteralField((int)metadataToken1, genericTypeArguments, genericMethodArguments));
     }
     catch (BadImageFormatException ex)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), (Exception)ex);
     }
 }
Ejemplo n.º 11
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);
            }
        }