Example #1
0
        // makes a copy of the method
        public void copyMethod(MethodDefinition dest, IMethodDefinition source)
        {
            // only copy body if it is not a dummy (= empty)
            if (!(source.Body is Microsoft.Cci.Dummy))
            {
                // copy instructions
                ILGenerator ilGenerator = new ILGenerator(this.host, dest);
                foreach (var operation in source.Body.Operations)
                {
                    ilGenerator.Emit(operation.OperationCode, operation.Value);
                }

                // copy the exception handler
                foreach (IOperationExceptionInformation exceptionToCopy in source.Body.OperationExceptionInformation)
                {
                    ILGeneratorLabel tryStart = new ILGeneratorLabel();
                    tryStart.Offset = exceptionToCopy.TryStartOffset;
                    ILGeneratorLabel tryEnd = new ILGeneratorLabel();
                    tryEnd.Offset = exceptionToCopy.TryEndOffset;
                    ILGeneratorLabel handlerStart = new ILGeneratorLabel();
                    handlerStart.Offset = exceptionToCopy.HandlerStartOffset;
                    ILGeneratorLabel handlerEnd = new ILGeneratorLabel();
                    handlerEnd.Offset = exceptionToCopy.HandlerEndOffset;
                    ILGeneratorLabel filterStart = new ILGeneratorLabel();
                    filterStart.Offset = exceptionToCopy.FilterDecisionStartOffset;

                    ilGenerator.AddExceptionHandlerInformation(exceptionToCopy.HandlerKind, exceptionToCopy.ExceptionType,
                                                               tryStart, tryEnd, handlerStart, handlerEnd, filterStart);
                }

                // create the body
                List <ILocalDefinition> variableListCopy           = new List <ILocalDefinition>(source.Body.LocalVariables);
                List <ITypeDefinition>  privateHelperTypesListCopy = new List <ITypeDefinition>(source.Body.PrivateHelperTypes);
                var newBody = new ILGeneratorMethodBody(ilGenerator, source.Body.LocalsAreZeroed, source.Body.MaxStack, dest,
                                                        variableListCopy, privateHelperTypesListCopy);
                dest.Body = newBody;
            }

            dest.CallingConvention = source.CallingConvention;
            if (source.IsGeneric)
            {
                dest.GenericParameters = new List <IGenericMethodParameter>(source.GenericParameters);
            }
            else
            {
                dest.GenericParameters = null;
            }
            if (source.ParameterCount > 0)
            {
                dest.Parameters = new List <IParameterDefinition>(source.Parameters);
            }
            else
            {
                dest.Parameters = null;
            }
            if (source.IsPlatformInvoke)
            {
                dest.PlatformInvokeData = source.PlatformInvokeData;
            }
            else
            {
                dest.PlatformInvokeData = Dummy.PlatformInvokeInformation;
            }
            dest.ReturnValueAttributes = new List <ICustomAttribute>(source.ReturnValueAttributes);
            if (source.ReturnValueIsModified)
            {
                dest.ReturnValueCustomModifiers = new List <ICustomModifier>(source.ReturnValueCustomModifiers);
            }
            else
            {
                dest.ReturnValueCustomModifiers = new List <ICustomModifier>(0);
            }
            if (source.ReturnValueIsMarshalledExplicitly)
            {
                dest.ReturnValueMarshallingInformation = source.ReturnValueMarshallingInformation;
            }
            else
            {
                dest.ReturnValueMarshallingInformation = Dummy.MarshallingInformation;
            }
            if (source.HasDeclarativeSecurity && IteratorHelper.EnumerableIsNotEmpty(source.SecurityAttributes))
            {
                dest.SecurityAttributes = new List <ISecurityAttribute>(source.SecurityAttributes);
            }
            else
            {
                dest.SecurityAttributes = null;
            }
            dest.Type = source.Type;
            dest.AcceptsExtraArguments     = source.AcceptsExtraArguments;
            dest.HasDeclarativeSecurity    = source.HasDeclarativeSecurity;
            dest.IsAbstract                = source.IsAbstract;
            dest.IsAccessCheckedOnOverride = source.IsAccessCheckedOnOverride;
            dest.IsCil                 = source.IsCil;
            dest.IsExternal            = source.IsExternal;
            dest.IsForwardReference    = source.IsForwardReference;
            dest.IsHiddenBySignature   = source.IsHiddenBySignature;
            dest.IsNativeCode          = source.IsNativeCode;
            dest.IsNewSlot             = source.IsNewSlot;
            dest.IsNeverInlined        = source.IsNeverInlined;
            dest.IsAggressivelyInlined = source.IsAggressivelyInlined;
            dest.IsNeverOptimized      = source.IsNeverOptimized;
            dest.IsPlatformInvoke      = source.IsPlatformInvoke;
            dest.IsRuntimeImplemented  = source.IsRuntimeImplemented;
            dest.IsRuntimeInternal     = source.IsRuntimeInternal;
            dest.IsRuntimeSpecial      = source.IsRuntimeSpecial;
            dest.IsSealed              = source.IsSealed;
            dest.IsSpecialName         = source.IsSpecialName;
            dest.IsStatic              = source.IsStatic;
            dest.IsSynchronized        = source.IsSynchronized;
            dest.IsUnmanaged           = source.IsUnmanaged;
            if (dest.IsStatic)
            {
                dest.IsVirtual = false;
            }
            else
            {
                dest.IsVirtual = source.IsVirtual;
            }
            dest.PreserveSignature                 = source.PreserveSignature;
            dest.RequiresSecurityObject            = source.RequiresSecurityObject;
            dest.ReturnValueIsByRef                = source.ReturnValueIsByRef;
            dest.ReturnValueIsMarshalledExplicitly = source.ReturnValueIsMarshalledExplicitly;
            dest.ReturnValueName = source.ReturnValueName;
            dest.Name            = source.Name;
            dest.Visibility      = source.Visibility;
        }