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


C# RuntimeFieldHandle.GetApproxDeclaringType方法代码示例

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


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

示例1: ResolveField

        public FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
        {
            MetadataToken tk = new MetadataToken(metadataToken);

            if (!MetadataImport.IsValidToken(tk))
                throw new ArgumentOutOfRangeException("metadataToken",
                    String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)));

            RuntimeTypeHandle[] typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
            RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);

            try 
            {
                RuntimeFieldHandle fieldHandle = new RuntimeFieldHandle();
            
                if (!tk.IsFieldDef)
                {
                    if (!tk.IsMemberRef)
                        throw new ArgumentException("metadataToken",
                            String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_ResolveField"), tk, this));

                    unsafe 
                    {
                        ConstArray sig = MetadataImport.GetMemberRefProps(tk);
                        
                        if (*(CorCallingConvention*)sig.Signature.ToPointer() != CorCallingConvention.Field)
                            throw new ArgumentException("metadataToken",
                                String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_ResolveField"), tk, this));                            
                    }

                    fieldHandle = GetModuleHandle().ResolveFieldHandle(tk, typeArgs, methodArgs);
                }

                fieldHandle = GetModuleHandle().ResolveFieldHandle(metadataToken, typeArgs, methodArgs);
                Type declaringType = fieldHandle.GetApproxDeclaringType().GetRuntimeType();
                
                if (declaringType.IsGenericType || declaringType.IsArray)
                {
                    int tkDeclaringType = GetModuleHandle().GetMetadataImport().GetParentToken(metadataToken);
                    declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
                }

                return System.RuntimeType.GetFieldInfo(declaringType.GetTypeHandleInternal(), fieldHandle);
            }
            catch(MissingFieldException)
            {
                return ResolveLiteralField(tk, genericTypeArguments, genericMethodArguments);
            }
            catch (BadImageFormatException e) 
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
            }           
        }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:53,代码来源:module.cs

示例2: ResolveField

 public unsafe FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
 {
     FieldInfo fieldInfo;
     System.Reflection.MetadataToken token = new System.Reflection.MetadataToken(metadataToken);
     if (!this.MetadataImport.IsValidToken((int) token))
     {
         throw new ArgumentOutOfRangeException("metadataToken", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidToken", new object[] { token, this }), new object[0]));
     }
     RuntimeTypeHandle[] typeInstantiationContext = ConvertToTypeHandleArray(genericTypeArguments);
     RuntimeTypeHandle[] methodInstantiationContext = ConvertToTypeHandleArray(genericMethodArguments);
     try
     {
         RuntimeFieldHandle fieldHandle = new RuntimeFieldHandle();
         if (!token.IsFieldDef)
         {
             if (!token.IsMemberRef)
             {
                 throw new ArgumentException("metadataToken", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_ResolveField"), new object[] { token, this }));
             }
             if (*(((byte*) this.MetadataImport.GetMemberRefProps((int) token).Signature.ToPointer())) != 6)
             {
                 throw new ArgumentException("metadataToken", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_ResolveField"), new object[] { token, this }));
             }
             fieldHandle = this.GetModuleHandle().ResolveFieldHandle((int) token, typeInstantiationContext, methodInstantiationContext);
         }
         fieldHandle = this.GetModuleHandle().ResolveFieldHandle(metadataToken, typeInstantiationContext, methodInstantiationContext);
         Type runtimeType = fieldHandle.GetApproxDeclaringType().GetRuntimeType();
         if (runtimeType.IsGenericType || runtimeType.IsArray)
         {
             int parentToken = this.GetModuleHandle().GetMetadataImport().GetParentToken(metadataToken);
             runtimeType = this.ResolveType(parentToken, genericTypeArguments, genericMethodArguments);
         }
         fieldInfo = System.RuntimeType.GetFieldInfo(runtimeType.GetTypeHandleInternal(), fieldHandle);
     }
     catch (MissingFieldException)
     {
         fieldInfo = this.ResolveLiteralField((int) token, genericTypeArguments, genericMethodArguments);
     }
     catch (BadImageFormatException exception)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), exception);
     }
     return fieldInfo;
 }
开发者ID:randomize,项目名称:VimConfig,代码行数:44,代码来源:Module.cs


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