/// <summary>Runs the specified from.</summary> /// <param name="from">From.</param> /// <param name="into">The into.</param> /// <param name="type">The type.</param> /// <param name="overwrite"> /// if set to <c>true</c> existing classes with conflicting /// names will be overwritten. /// </param> /// <returns>A collection of exceptions.</returns> public static IEnumerable <Exception> Run( AssemblyDefinition from, AssemblyDefinition into, TypeReference type, bool overwrite) { var worker = new TemplateCopy(from, into, type, overwrite); worker.Run(); return(worker.GetExceptions()); }
/// <summary>Injects the AsmZ (embedded dll) resolver.</summary> public void InjectAsmZResolver() { const string typeLibZInitializer = "LibZ.Injected.LibZInitializer"; var initializerType = _targetAssembly.MainModule.Types.Single(t => t.FullName == typeLibZInitializer); var initializerMethod = initializerType.Methods.Single(m => m.Name == "InitializeAsmZ"); var body = initializerMethod.Body.Instructions; body.Clear(); const string typeAsmZResolver = "LibZ.Injected.AsmZResolver"; var sourceType = _sourceAssembly.MainModule.Types.Single(t => t.FullName == typeAsmZResolver); TemplateCopy.Run(_sourceAssembly, _targetAssembly, sourceType, false); var targetType = _targetAssembly.MainModule.Types.Single(t => t.FullName == typeAsmZResolver); var targetMethod = targetType.Methods.Single(m => m.Name == "Initialize"); body.Add(Instruction.Create(OpCodes.Call, targetMethod)); body.Add(Instruction.Create(OpCodes.Ret)); }
/// <summary>Injects the LibZInitializer.</summary> public void InjectLibZInitializer() { const string typeName = "LibZ.Injected.LibZInitializer"; var targetType = _targetAssembly.MainModule.Types.SingleOrDefault(t => t.FullName == typeName); if (targetType != null) { return; } var sourceType = _sourceAssembly.MainModule.Types.Single(t => t.FullName == typeName); TemplateCopy.Run(_sourceAssembly, _targetAssembly, sourceType, false); targetType = _targetAssembly.MainModule.Types.Single(t => t.FullName == typeName); var targetMethod = targetType.Methods.Single(m => m.Name == "Initialize"); // find 'module' static constructor var moduleType = _targetAssembly.MainModule.Types.Single(t => t.FullName == "<Module>"); var moduleCtor = moduleType.Methods.SingleOrDefault(m => m.Name == ".cctor"); // if module static constructor has not been found create it if (moduleCtor == null) { // private hidebysig specialname rtspecialname static - at least that's what other static constructors have const MethodAttributes attributes = MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.HideBySig; moduleCtor = new MethodDefinition(".cctor", attributes, _targetAssembly.MainModule.TypeSystem.Void); moduleCtor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); moduleType.Methods.Add(moduleCtor); } // whenever it was just created or existed before - inject LibZ initialization into it moduleCtor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, targetMethod)); }
/// <summary>Runs the specified from.</summary> /// <param name="from">From.</param> /// <param name="into">The into.</param> /// <param name="type">The type.</param> /// <param name="overwrite"> /// if set to <c>true</c> existing classes with conflicting /// names will be overwritten. /// </param> /// <returns>A collection of exceptions.</returns> public static IEnumerable<Exception> Run( AssemblyDefinition from, AssemblyDefinition into, TypeReference type, bool overwrite) { var worker = new TemplateCopy(from, into, type, overwrite); worker.Run(); return worker.GetExceptions(); }