/// <inheritdoc />
        protected override CallingConventionSignature GetSignature()
        {
            var reader = _parentModule.DotNetDirectory.Metadata
                         .GetStream <BlobStream>()
                         .GetBlobReaderByIndex(_row.Signature);

            return(CallingConventionSignature.FromReader(_parentModule, reader, true));
        }
Example #2
0
        public static void ReadLocalVariables(this CilMethodBody methodBody, MethodDefinition method, byte[] localSig)
        {
            var locals = CallingConventionSignature.FromReader(method.Module, new ByteArrayReader(localSig)) as LocalVariablesSignature;

            for (var i = 0; i < locals?.VariableTypes.Count; i++)
            {
                methodBody.LocalVariables.Add(new CilLocalVariable(locals.VariableTypes[i]));
            }
        }
Example #3
0
 internal StandAloneSignature(MetadataHeader header, MetadataToken token, MetadataRow <uint> row)
     : base(header, token, row)
 {
     _signature = new LazyValue <CallingConventionSignature>(() =>
     {
         IBinaryStreamReader reader;
         return(header.GetStream <BlobStream>().TryCreateBlobReader(row.Column1, out reader)
             ? CallingConventionSignature.FromReader(header, reader)
             : null);
     });
 }
        internal StandAloneSignature(MetadataImage image, MetadataRow <uint> row)
            : base(row.MetadataToken)
        {
            Image      = image;
            _signature = new LazyValue <CallingConventionSignature>(() =>
                                                                    image.Header.GetStream <BlobStream>().TryCreateBlobReader(row.Column1, out var reader)
                    ? CallingConventionSignature.FromReader(image, reader, true)
                    : null);

            CustomAttributes = new CustomAttributeCollection(this);
        }
Example #5
0
 internal StandAloneSignature(MetadataImage image, MetadataRow <uint> row)
     : base(row.MetadataToken)
 {
     _image     = image;
     _signature = new LazyValue <CallingConventionSignature>(() =>
     {
         IBinaryStreamReader reader;
         return(image.Header.GetStream <BlobStream>().TryCreateBlobReader(row.Column1, out reader)
             ? CallingConventionSignature.FromReader(image, reader)
             : null);
     });
 }
Example #6
0
        public static void ReadLocalVariables(CilMethodBody methodBody, MethodDefinition method, byte[] localSig)
        {
            if (!(method.Module is SerializedModuleDefinition module))
            {
                throw new ArgumentException("Method body should reference a serialized module.");
            }

            var localsSignature = (LocalVariablesSignature)CallingConventionSignature.FromReader(
                new BlobReadContext(module.ReaderContext),
                new ByteArrayReader(localSig));

            for (int i = 0; i < localsSignature?.VariableTypes.Count; i++)
            {
                methodBody.LocalVariables.Add(new CilLocalVariable(localsSignature.VariableTypes[i]));
            }
        }
Example #7
0
        internal MemberReference(MetadataHeader header, MetadataToken token, MetadataRow <uint, uint, uint> row)
            : base(header, token, row)
        {
            var tableStream = header.GetStream <TableStream>();

            _parent = new LazyValue <IMemberRefParent>(() =>
            {
                var parentToken = tableStream.GetIndexEncoder(CodedIndex.MemberRefParent).DecodeIndex(row.Column1);
                return(parentToken.Rid != 0 ? (IMemberRefParent)tableStream.ResolveMember(parentToken) : null);
            });

            _name = new LazyValue <string>(() => header.GetStream <StringStream>().GetStringByOffset(row.Column2));

            _signature = new LazyValue <MemberSignature>(() =>
                                                         CallingConventionSignature.FromReader(header, header.GetStream <BlobStream>().CreateBlobReader(row.Column3)) as MemberSignature);
        }
Example #8
0
        public MemberReference(MetadataImage image, MetadataRow <uint, uint, uint> row)
            : base(row.MetadataToken)
        {
            _image = image;
            var tableStream = image.Header.GetStream <TableStream>();

            _parent = new LazyValue <IMemberRefParent>(() =>
            {
                var parentToken = tableStream.GetIndexEncoder(CodedIndex.MemberRefParent).DecodeIndex(row.Column1);
                return(parentToken.Rid != 0 ? (IMemberRefParent)image.ResolveMember(parentToken) : null);
            });

            _name = new LazyValue <string>(() => image.Header.GetStream <StringStream>().GetStringByOffset(row.Column2));

            _signature = new LazyValue <MemberSignature>(() =>
                                                         CallingConventionSignature.FromReader(image, image.Header.GetStream <BlobStream>().CreateBlobReader(row.Column3)) as MemberSignature);

            CustomAttributes = new CustomAttributeCollection(this);
        }
Example #9
0
        private static object ReadToken(ModuleReaderContext readerContext, MetadataToken token, IList <object> tokens, ReferenceImporter importer)
        {
            switch (token.Table)
            {
            case TableIndex.TypeDef:
                var type = tokens[(int)token.Rid];
                if (type is RuntimeTypeHandle runtimeTypeHandle)
                {
                    return(importer.ImportType(Type.GetTypeFromHandle(runtimeTypeHandle)));
                }
                break;

            case TableIndex.Field:
                var field = tokens[(int)token.Rid];
                if (field is null)
                {
                    return(null);
                }
                if (field is RuntimeFieldHandle runtimeFieldHandle)
                {
                    return(importer.ImportField(FieldInfo.GetFieldFromHandle(runtimeFieldHandle)));
                }

                if (field.GetType().FullName == "System.Reflection.Emit.GenericFieldInfo")
                {
                    var result = FieldReader.TryReadField <RuntimeFieldHandle>(field, "m_field", out var mField);
                    var ctx    = FieldReader.ReadField <RuntimeTypeHandle>(field, "m_context");
                    return(importer.ImportField(FieldInfo.GetFieldFromHandle(result
                            ? mField
                            : FieldReader.ReadField <RuntimeFieldHandle>(field, "m_fieldHandle"), ctx)));
                }

                break;

            case TableIndex.Method:
            case TableIndex.MemberRef:
                var obj = tokens[(int)token.Rid];
                if (obj is RuntimeMethodHandle methodHandle)
                {
                    return(importer.ImportMethod(MethodBase.GetMethodFromHandle(methodHandle)));
                }
                if (obj.GetType().FullName == "System.Reflection.Emit.GenericMethodInfo")
                {
                    var context =
                        FieldReader.ReadField <RuntimeTypeHandle>(obj, "m_context");
                    var res      = FieldReader.TryReadField <RuntimeMethodHandle>(obj, "m_method", out var m_method);
                    var m_handle = FieldReader.ReadField <RuntimeMethodHandle>(obj, "m_methodHandle");
                    var method   = MethodBase.GetMethodFromHandle(
                        res
                                ? m_method
                                : m_handle, context);
                    return(importer.ImportMethod(method));
                }

                if (obj.GetType().FullName == "System.Reflection.Emit.VarArgMethod")
                {
                    return(importer.ImportMethod(FieldReader.ReadField <MethodInfo>(obj, "m_method")));
                }
                break;

            case TableIndex.StandAloneSig:
                return(CallingConventionSignature.FromReader(
                           new BlobReadContext(readerContext),
                           new ByteArrayReader((byte[])tokens[(int)token.Rid])));

            case (TableIndex)112:
                return(tokens[(int)token.Rid] as string);
            }

            return(null);
        }
        /// <inheritdoc />
        public override object ResolveMember(MetadataToken token)
        {
            switch (token.Table)
            {
            case TableIndex.TypeDef:
                var type = _tokens[(int)token.Rid];
                if (type is RuntimeTypeHandle runtimeTypeHandle)
                {
                    return(_importer.ImportType(Type.GetTypeFromHandle(runtimeTypeHandle)));
                }
                break;

            case TableIndex.Field:
                var field = _tokens[(int)token.Rid];

                if (field is null)
                {
                    return(null);
                }

                if (field is RuntimeFieldHandle runtimeFieldHandle)
                {
                    return(_importer.ImportField(FieldInfo.GetFieldFromHandle(runtimeFieldHandle)));
                }

                if (field.GetType().FullName == "System.Reflection.Emit.GenericFieldInfo")
                {
                    bool result = FieldReader.TryReadField <RuntimeFieldHandle>(field, "m_field", out var mField);
                    var  ctx    = FieldReader.ReadField <RuntimeTypeHandle>(field, "m_context");
                    return(_importer.ImportField(FieldInfo.GetFieldFromHandle(result
                            ? mField
                            : FieldReader.ReadField <RuntimeFieldHandle>(field, "m_fieldHandle"), ctx)));
                }

                break;

            case TableIndex.Method:
            case TableIndex.MemberRef:
                var obj = _tokens[(int)token.Rid];

                if (obj is RuntimeMethodHandle methodHandle)
                {
                    return(_importer.ImportMethod(MethodBase.GetMethodFromHandle(methodHandle)));
                }

                if (obj.GetType().FullName == "System.Reflection.Emit.GenericMethodInfo")
                {
                    var  context   = FieldReader.ReadField <RuntimeTypeHandle>(obj, "m_context");
                    bool hasHandle = FieldReader.TryReadField <RuntimeMethodHandle>(obj, "m_method", out var mMethod);
                    var  mHandle   = FieldReader.ReadField <RuntimeMethodHandle>(obj, "m_methodHandle");
                    var  method    = MethodBase.GetMethodFromHandle(
                        hasHandle ? mMethod : mHandle,
                        context);

                    return(_importer.ImportMethod(method));
                }

                if (obj.GetType().FullName == "System.Reflection.Emit.VarArgMethod")
                {
                    return(_importer.ImportMethod(FieldReader.ReadField <MethodInfo>(obj, "m_method")));
                }

                break;

            case TableIndex.StandAloneSig:
                var reader = ByteArrayDataSource.CreateReader((byte[])_tokens[(int)token.Rid]);
                return(CallingConventionSignature.FromReader(new BlobReadContext(_readerContext), ref reader));
            }

            return(token);
        }