示例#1
1
			protected override void SetIL (MethodBuilder methodBuilder, MemoryStream ilStream)
			{
				methodBuilder.SetMethodBody (ilStream.ToArray (), 999, null, null, null);
			}
        [System.Security.SecurityCritical] // auto-generated
        #endif
        public void SetMethodBody(byte[] il, int maxStack, byte[] localSignature, IEnumerable <ExceptionHandler> exceptionHandlers, IEnumerable <int> tokenFixups)
        {
            if (m_isDefaultConstructor)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DefaultConstructorDefineBody"));
            }

            m_methodBuilder.SetMethodBody(il, maxStack, localSignature, exceptionHandlers, tokenFixups);
        }
示例#3
0
        private void EmitMethodBody(MethodBuilder methodBuilder, ConstructorBuilder constructorBuilder, Cci.IMethodBody body)
        {
            byte[] byteCode = body.IL.Copy(0, body.IL.Length);
            int[] tokenFixupOffsets = SubstituteTokens(byteCode);

            // prepare signature:
            var sigHelper = SignatureHelper.GetLocalVarSigHelper(_builder);
            int localCount = 0;
            foreach (var local in body.LocalVariables)
            {
                var type = ResolveType(local.Type);
                if (local.IsReference)
                {
                    type = type.MakeByRefType();
                }

                if (local.CustomModifiers.Any())
                {
                    if (local.IsPinned)
                    {
                        throw new NotSupportedException("Ref.Emit limitation");
                    }

                    Type[] reqMods, optMods;
                    ResolveCustomModifiers(local.CustomModifiers, out reqMods, out optMods);
                    sigHelper.AddArgument(type, reqMods, optMods);
                }
                else
                {
                    sigHelper.AddArgument(type, local.IsPinned);
                }

                localCount++;
            }

            byte[] signature = sigHelper.GetSignature();

            // prepare exception handlers:
            var regions = body.ExceptionRegions;
            var handlers = new ExceptionHandler[regions.Length];
            int i = 0;
            foreach (var info in regions)
            {
                handlers[i++] = new ExceptionHandler(
                    tryOffset: (int)info.TryStartOffset,
                    tryLength: (int)info.TryEndOffset - (int)info.TryStartOffset,
                    filterOffset: (int)info.FilterDecisionStartOffset,
                    handlerOffset: (int)info.HandlerStartOffset,
                    handlerLength: (int)info.HandlerEndOffset - (int)info.HandlerStartOffset,
                    kind: (ExceptionHandlingClauseOptions)info.HandlerKind,
                    exceptionTypeToken: (info.HandlerKind == ExceptionRegionKind.Catch) ? GetToken(ResolveType(info.ExceptionType)) : 0);
            }

            if (methodBuilder != null)
            {
                methodBuilder.InitLocals = body.LocalsAreZeroed;
                methodBuilder.SetMethodBody(byteCode, body.MaxStack, signature, handlers, tokenFixupOffsets);
            }
            else
            {
                constructorBuilder.InitLocals = body.LocalsAreZeroed;
                constructorBuilder.SetMethodBody(byteCode, body.MaxStack, signature, handlers, tokenFixupOffsets);
            }
        }