Ejemplo n.º 1
0
        private void CopyConstructorInitInstructions()
        {
            var constructors = GetConstructors(DestinationType, true);

            var visitor = new ObservableAstVisitor();

            bool isNewConstructor = false;

            visitor.EnterConstructorDeclaration += exp => {
                var constructor = exp.Annotation <MethodReference>();
                if (!copier.CopyMap.ContainsKey(constructor))
                {
                    isNewConstructor = true;
                }
            };
            visitor.LeaveConstructorDeclaration += exp => {
                isNewConstructor = false;
            };
            visitor.EnterConstructorInitializer += exp => {
                isNewConstructor = false;
            };
            visitor.EnterAssignmentExpression += exp => {
                var member = exp.Left.Annotation <MemberReference>();
                if (isNewConstructor)
                {
                    if (copier.CopyMap.ContainsKey(member))
                    {
                        var argILRange      = exp.Annotation <List <ILRange> >();
                        var newInstructions = GetInstructions(exp);
                        foreach (var constructor in constructors)
                        {
                            var newInstruction = GetInstruction(newInstructions, argILRange[0].To);
                            newInstruction = newInstruction.Previous;
                            while (newInstruction != null && newInstruction.Offset >= argILRange[0].From)
                            {
                                constructor.Body.Instructions.Insert(0, copier.Copy(newInstruction));
                                newInstruction = newInstruction.Previous;
                            }
                        }
                        Console.WriteLine("Copied to " + constructors.Count + " constructor assigment of " + member);
                    }
                }
            };

            copier.Process();

            var d = new DecompilerContext(OriginType.Module)
            {
                CurrentType = OriginType
            };
            var astBuilder = new AstBuilder(d);

            astBuilder.AddType(OriginType);
            astBuilder.SyntaxTree.AcceptVisitor(visitor);
        }
Ejemplo n.º 2
0
        public void AddAssembly(string origAssemblyPath)
        {
            var origAssembly = CecilUtils.GetAssemblyDef(origAssemblyPath);
            var copier       = new CecilCopier(origAssembly.MainModule, destAssembly.MainModule);

            foreach (var origType in origAssembly.MainModule.Types)
            {
                if (origType.BaseType != null)
                {
                    copier.Copy(origType);
                }
            }

            copier.Process();
        }