Beispiel #1
0
        public int AddMethodBody(MethodBase methodBase, StandaloneSignatureHandle localVariablesSignature = default)
        {
            var body = methodBase.GetMethodBody();

            if (body == null)
            {
                return(_metadata.ILBuilder.Count);
            }

            var localVariables = body.LocalVariables.ToArray();
            var localEncoder   = new BlobEncoder(new BlobBuilder()).LocalVariableSignature(localVariables.Length);

            localEncoder.AddRange(localVariables, _metadata);

            var instructions              = methodBase.GetInstructions();
            var maxStack                  = body.MaxStackSize;
            var codeSize                  = body.GetILAsByteArray().Length;
            var exceptionRegionCount      = body.ExceptionHandlingClauses.Count;
            var attributes                = body.InitLocals ? MethodBodyAttributes.InitLocals : MethodBodyAttributes.None;
            var hasDynamicStackAllocation = instructions.Any(x => x.OpCode == OpCodes.Localloc);

            // Header
            var offset = SerializeHeader(codeSize, maxStack, exceptionRegionCount, attributes, localVariablesSignature, hasDynamicStackAllocation);

            // Instructions
            MethodBodyWriter.Write(_metadata, instructions);

            // Exceptions
            SerializeExceptionRegions(body);

            return(offset);
        }
        public int AddMethodBody(MethodBase methodBase)
        {
            var body = methodBase.GetMethodBody();

            if (body == null)
            {
                return(_builder.Count);
            }

            var instructions              = methodBase.GetInstructions();
            var maxStack                  = body.MaxStackSize;
            var codeSize                  = body.GetILAsByteArray().Length;
            var exceptionRegionCount      = body.ExceptionHandlingClauses.Count;
            var attributes                = body.InitLocals ? MethodBodyAttributes.InitLocals : MethodBodyAttributes.None;
            var localVariablesSignature   = default(StandaloneSignatureHandle);
            var hasDynamicStackAllocation = instructions.Any(x => x.OpCode == OpCodes.Localloc);

            var offset = SerializeHeader(codeSize, maxStack, exceptionRegionCount, attributes, localVariablesSignature,
                                         hasDynamicStackAllocation);

            MethodBodyWriter.Write(_builder, instructions, _stringAccessor, _typeHandles, _ctorRefHandles,
                                   _fieldHandles, _methodHandles);
            return(offset);
        }