Beispiel #1
0
 private void DeepCopyTypes(MemberCloneContext context)
 {
     foreach (var type in _typesToClone)
     {
         DeepCopyType(context, type);
     }
 }
Beispiel #2
0
 private void CreateTypeStubs(MemberCloneContext context)
 {
     foreach (var type in _typesToClone)
     {
         CreateTypeStub(context, type);
     }
 }
 private void CloneParameterDefinitionsInMethod(MemberCloneContext context, MethodDefinition method, MethodDefinition clonedMethod)
 {
     foreach (var parameterDef in method.ParameterDefinitions)
     {
         clonedMethod.ParameterDefinitions.Add(CloneParameterDefinition(context, parameterDef));
     }
 }
 private void DeepCopyMethods(MemberCloneContext context)
 {
     foreach (var method in _methodsToClone)
     {
         DeepCopyMethod(context, method);
     }
 }
 private void CloneLocalVariables(MemberCloneContext context, CilMethodBody body, CilMethodBody clonedBody)
 {
     foreach (var variable in body.LocalVariables)
     {
         var clonedVariable = new CilLocalVariable(context.Importer.ImportTypeSignature(variable.VariableType));
         clonedBody.LocalVariables.Add(clonedVariable);
     }
 }
Beispiel #6
0
 private void DeepCopyMembers(MemberCloneContext context)
 {
     DeepCopyTypes(context);
     DeepCopyMethods(context);
     DeepCopyFields(context);
     DeepCopyProperties(context);
     DeepCopyEvents(context);
 }
Beispiel #7
0
        /// <summary>
        /// Clones all included members.
        /// </summary>
        /// <returns>An object representing the result of the cloning process.</returns>
        public MemberCloneResult Clone()
        {
            var context = new MemberCloneContext(_targetModule);

            CreateMemberStubs(context);
            DeepCopyMembers(context);

            return(new MemberCloneResult(context.ClonedMembers));
        }
        private ParameterDefinition CloneParameterDefinition(MemberCloneContext context, ParameterDefinition parameterDef)
        {
            var clonedParameterDef = new ParameterDefinition(parameterDef.Sequence, parameterDef.Name, parameterDef.Attributes);

            CloneCustomAttributes(context, parameterDef, clonedParameterDef);
            clonedParameterDef.Constant          = CloneConstant(context, parameterDef.Constant);
            clonedParameterDef.MarshalDescriptor = CloneMarshalDescriptor(context, parameterDef.MarshalDescriptor);
            return(clonedParameterDef);
        }
        private MethodDefinition CreateMethodStub(MemberCloneContext context, MethodDefinition method)
        {
            var clonedMethod = new MethodDefinition(method.Name, method.Attributes,
                                                    context.Importer.ImportMethodSignature(method.Signature));

            clonedMethod.ImplAttributes = method.ImplAttributes;

            clonedMethod.Parameters.PullUpdatesFromMethodSignature();

            context.ClonedMembers[method] = clonedMethod;
            return(clonedMethod);
        }
        private void CreateMethodStubs(MemberCloneContext context)
        {
            foreach (var method in _methodsToClone)
            {
                var stub = CreateMethodStub(context, method);

                // If method's declaring type is cloned as well, add the cloned method to the cloned type.
                if (context.ClonedMembers.TryGetValue(method.DeclaringType, out var member) &&
                    member is TypeDefinition declaringType)
                {
                    declaringType.Methods.Add(stub);
                }
            }
        }
        private void DeepCopyMethod(MemberCloneContext context, MethodDefinition method)
        {
            var clonedMethod = (MethodDefinition)context.ClonedMembers[method];

            CloneParameterDefinitionsInMethod(context, method, clonedMethod);

            if (method.CilMethodBody != null)
            {
                clonedMethod.CilMethodBody = CloneCilMethodBody(context, method);
            }

            CloneCustomAttributes(context, method, clonedMethod);
            CloneGenericParameters(context, method, clonedMethod);
            CloneSecurityDeclarations(context, method, clonedMethod);

            clonedMethod.ImplementationMap = CloneImplementationMap(context, method.ImplementationMap);
        }
        private CilMethodBody CloneCilMethodBody(MemberCloneContext context, MethodDefinition method)
        {
            var body = method.CilMethodBody;

            var clonedMethod = (MethodDefinition)context.ClonedMembers[method];

            // Clone method body header.
            var clonedBody = new CilMethodBody(clonedMethod);

            clonedBody.InitializeLocals = body.InitializeLocals;
            clonedBody.MaxStack         = body.MaxStack;

            // Clone contents.
            CloneLocalVariables(context, body, clonedBody);
            CloneCilInstructions(context, body, clonedBody);
            CloneExceptionHandlers(context, body, clonedBody);

            return(clonedBody);
        }
        private CilInstruction CloneInstruction(MemberCloneContext context, CilMethodBody clonedBody, CilInstruction instruction)
        {
            var clonedInstruction = new CilInstruction(instruction.Offset, instruction.OpCode);

            switch (instruction.OpCode.OperandType)
            {
            case CilOperandType.InlineBrTarget:
            case CilOperandType.ShortInlineBrTarget:
            case CilOperandType.InlineSwitch:
                // Fix up later when all instructions are added.
                clonedInstruction.Operand = instruction.Operand;
                break;

            case CilOperandType.InlineI:
            case CilOperandType.InlineI8:
            case CilOperandType.InlineNone:
            case CilOperandType.InlineR:
            case CilOperandType.InlineString:
            case CilOperandType.ShortInlineI:
            case CilOperandType.ShortInlineR:
                clonedInstruction.Operand = instruction.Operand;
                break;

            case CilOperandType.InlineField:
                clonedInstruction.Operand = context.Importer.ImportField((IFieldDescriptor)instruction.Operand);
                break;

            case CilOperandType.InlineMethod:
                clonedInstruction.Operand = context.Importer.ImportMethod((IMethodDescriptor)instruction.Operand);
                break;

            case CilOperandType.InlineSig:
                if (instruction.Operand is StandAloneSignature standalone)
                {
                    instruction.Operand = new StandAloneSignature(standalone.Signature switch
                    {
                        MethodSignature signature => context.Importer.ImportMethodSignature(signature),
                        GenericInstanceMethodSignature signature => context.Importer.ImportGenericInstanceMethodSignature(signature),
                        _ => throw new NotImplementedException()
                    });
                }
        private static void CloneCilInstructions(MemberCloneContext context, CilMethodBody body, CilMethodBody clonedBody)
        {
            var branches = new List <CilInstruction>();
            var switches = new List <CilInstruction>();

            // Clone all instructions.
            foreach (var instruction in body.Instructions)
            {
                var clonedInstruction = CloneInstruction(context, clonedBody, instruction);
                if (clonedInstruction.OpCode.Code == CilCode.Switch)
                {
                    switches.Add(clonedInstruction);
                }
                else if (clonedInstruction.IsBranch())
                {
                    branches.Add(clonedInstruction);
                }
                clonedBody.Instructions.Add(clonedInstruction);
            }

            // Fixup branches.
            foreach (var branch in branches)
            {
                var label = (ICilLabel)branch.Operand;
                branch.Operand = new CilInstructionLabel(clonedBody.Instructions.GetByOffset(label.Offset));
            }

            // Fixup switches.
            foreach (var @switch in switches)
            {
                var labels       = (IList <ICilLabel>)@switch.Operand;
                var clonedLabels = new List <ICilLabel>(labels.Count);

                for (int i = 0; i < labels.Count; i++)
                {
                    clonedLabels.Add(clonedBody.Instructions.GetLabel(labels[i].Offset));
                }

                @switch.Operand = clonedLabels;
            }
        }
Beispiel #15
0
 private void CreateMemberStubs(MemberCloneContext context)
 {
     CreateTypeStubs(context);
     CreateMethodStubs(context);
     CreateFieldStubs(context);
 }
Beispiel #16
0
        private void DeepCopyType(MemberCloneContext context, TypeDefinition type)
        {
            var clonedType = (TypeDefinition)context.ClonedMembers[type];

            // Copy base type.
            if (type.BaseType is {})
Beispiel #17
0
        private void CreateTypeStub(MemberCloneContext context, TypeDefinition type)
        {
            var typeStub = new TypeDefinition(type.Namespace, type.Name, type.Attributes);

            context.ClonedMembers.Add(type, typeStub);
        }