Example #1
0
        /// <inheritdoc />
        public MethodBody ReadMethodBody(MethodDefinition owner, MethodDefinitionRow row)
        {
            try
            {
                if (row.Body.CanRead)
                {
                    if (owner.IsIL)
                    {
                        var rawBody = CilRawMethodBody.FromReader(row.Body.CreateReader());
                        return(CilMethodBody.FromRawMethodBody(owner, rawBody));
                    }
                    else
                    {
                        // TODO: handle native method bodies.
                    }
                }
                else if (row.Body.IsBounded && row.Body.GetSegment() is CilRawMethodBody rawMethodBody)
                {
                    return(CilMethodBody.FromRawMethodBody(owner, rawMethodBody));
                }
            }
            catch when(!ThrowOnInvalidMethodBody)
            {
                return(null);
            }

            return(null);
        }
Example #2
0
        internal MethodDefinition(MetadataImage image, MetadataRow <FileSegment, MethodImplAttributes, MethodAttributes, uint, uint, uint> row)
            : base(row.MetadataToken)
        {
            _image = image;
            var stringStream = image.Header.GetStream <StringStream>();
            var blobStream   = image.Header.GetStream <BlobStream>();

            ImplAttributes = row.Column2;
            Attributes     = row.Column3;
            _name          = new LazyValue <string>(() => stringStream.GetStringByOffset(row.Column4));

            IBinaryStreamReader blobReader;

            if (blobStream.TryCreateBlobReader(row.Column5, out blobReader))
            {
                _signature = new LazyValue <MethodSignature>(() => MethodSignature.FromReader(image, blobReader));
            }

            _methodBody = new LazyValue <MethodBody>(() =>
            {
                var rawBody = row.Column1 as CilRawMethodBody;
                if (rawBody != null)
                {
                    return(CilMethodBody.FromRawMethodBody(this, rawBody));
                }

                // TODO: handler for native methods.
                return(null);
            });

            _declaringType = new LazyValue <TypeDefinition>(() =>
            {
                var table   = image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.TypeDef);
                var typeRow = table.GetRowClosestToKey(5, row.MetadataToken.Rid);
                return((TypeDefinition)table.GetMemberFromRow(image, typeRow));
            });

            _pinvokeMap = new LazyValue <ImplementationMap>(() =>
            {
                if (!row.Column3.HasFlag(MethodAttributes.PInvokeImpl))
                {
                    return(null);
                }

                var table  = (ImplementationMapTable)image.Header.GetStream <TableStream>().GetTable(MetadataTokenType.ImplMap);
                var mapRow = table.FindImplementationMapOfOwner(row.MetadataToken);
                return(mapRow != null ? (ImplementationMap)table.GetMemberFromRow(image, mapRow) : null);
            });

            Parameters = new RangedMemberCollection <MethodDefinition, ParameterDefinition>(this, MetadataTokenType.Param, 5, GetParamOwner, SetParamOwner);

            CustomAttributes     = new CustomAttributeCollection(this);
            SecurityDeclarations = new SecurityDeclarationCollection(this);
            GenericParameters    = new GenericParameterCollection(this);
        }