Exemplo n.º 1
0
        internal static void Manage(Action <AssemblyDefinition> addTo,
                                    Action <AssemblyDefinition> modify, bool addDebugSymbols)
        {
            var assemblyFileName = AssemblyManager.Create(addTo, addDebugSymbols);

            try
            {
                var assembly = AssemblyDefinition.ReadAssembly(assemblyFileName);
                modify(assembly);
                assembly.Write(assemblyFileName);
            }
            finally
            {
                AssemblyManager.RemoveFile(assemblyFileName);
            }
        }
Exemplo n.º 2
0
        public void RunWhenTypeHasAttribute()
        {
            var typeName = Guid.NewGuid().ToString("N");

            AssemblyManager.ManageFile((assembly) =>
            {
                var toStringAttributeCtor = assembly.MainModule.Import(
                    typeof(ToStringAttribute).GetConstructor(Type.EmptyTypes));
                var attribute = new CustomAttribute(toStringAttributeCtor);

                var type = AssemblyManager.AddType(assembly, typeName);
                type.CustomAttributes.Add(attribute);
            }, (file) =>
            {
                var beforeAssembly = AssemblyDefinition.ReadAssembly(file.FullName);
                var beforeType     = (from type in beforeAssembly.MainModule.GetAllTypes()
                                      where type.Name == typeName
                                      select type).First();

                Assert.AreEqual(0, beforeType.Methods.Count());

                InjectorRunner.Run(file);

                var afterAssembly = AssemblyDefinition.ReadAssembly(file.FullName);
                var afterType     = (from type in afterAssembly.MainModule.GetAllTypes()
                                     where type.Name == typeName
                                     select type).First();

                var methods = afterType.Methods.ToList();
                Assert.AreEqual(1, methods.Count);

                var instructions = methods[0].Body.Instructions;
                Assert.AreEqual(2, instructions.Count);
                Assert.AreEqual(OpCodes.Ldstr, instructions[0].OpCode);
                Assert.AreEqual(OpCodes.Ret, instructions[1].OpCode);
            }, false);
        }
Exemplo n.º 3
0
 internal static MethodDefinition AddMethod(TypeDefinition type, string methodName)
 {
     return(AssemblyManager.AddMethod(type, methodName,
                                      new List <ParameterDefinition>(), null));
 }