private void BuildAssembly(AppDomain appDomain, string dir, string filename) { // Prepare the assembly-level permissions. PermissionSet requiredPermissions = new PermissionSet(null); PermissionSet optionalPermissions = new PermissionSet(null); PermissionSet refusedPermissions = new PermissionSet(null); foreach (SecurityDeclaration securityDeclaration in assemblyDefinition.SecurityDeclarations) { switch (securityDeclaration.Action) { case Mono.Cecil.SecurityAction.RequestMinimum: requiredPermissions = requiredPermissions.Union(securityDeclaration.PermissionSet); break; case Mono.Cecil.SecurityAction.RequestOptional: optionalPermissions = optionalPermissions.Union(securityDeclaration.PermissionSet); break; case Mono.Cecil.SecurityAction.RequestRefuse: refusedPermissions = refusedPermissions.Union(securityDeclaration.PermissionSet); break; } } // Build the dynamic assembly. AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly( new AssemblyName(assemblyDefinition.Name.FullName), AssemblyBuilderAccess.RunAndSave, dir, requiredPermissions, optionalPermissions, refusedPermissions); dynamicAssembly = new DynamicAssembly(assemblyBuilder, filename); // TODO: Set entry point and assembly kind. foreach (ModuleDefinition moduleDefinition in assemblyDefinition.Modules) { BuildModule(moduleDefinition); } metadataPass.Add(delegate { InitializeCustomAttributes(assemblyBuilder.SetCustomAttribute, assemblyDefinition.CustomAttributes); }); }
/// <inheritdoc /> public Assembly InstrumentAndLoad(string assemblyPath, string instrumentedAssemblySavePath) { if (assemblyPath == null) { throw new ArgumentNullException(@"assemblyPath"); } AssemblyDefinition assemblyDefinition = AssemblyFactory.GetAssembly(assemblyPath); DynamicAssemblyBuilder builder = new DynamicAssemblyBuilder(assemblyDefinition); DynamicAssembly dynamicAssembly = builder.Build(instrumentedAssemblySavePath); if (instrumentedAssemblySavePath != null) { dynamicAssembly.Save(); } return(dynamicAssembly.Builder); }
public DynamicModule(ModuleBuilder builder, DynamicAssembly assembly) { this.builder = builder; this.assembly = assembly; }
private void BuildAssembly(AppDomain appDomain, string dir, string filename) { // Prepare the assembly-level permissions. PermissionSet requiredPermissions = new PermissionSet(null); PermissionSet optionalPermissions = new PermissionSet(null); PermissionSet refusedPermissions = new PermissionSet(null); foreach (SecurityDeclaration securityDeclaration in assemblyDefinition.SecurityDeclarations) { switch (securityDeclaration.Action) { case Mono.Cecil.SecurityAction.RequestMinimum: requiredPermissions = requiredPermissions.Union(securityDeclaration.PermissionSet); break; case Mono.Cecil.SecurityAction.RequestOptional: optionalPermissions = optionalPermissions.Union(securityDeclaration.PermissionSet); break; case Mono.Cecil.SecurityAction.RequestRefuse: refusedPermissions = refusedPermissions.Union(securityDeclaration.PermissionSet); break; } } // Build the dynamic assembly. AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly( new AssemblyName(assemblyDefinition.Name.FullName), AssemblyBuilderAccess.RunAndSave, dir, requiredPermissions, optionalPermissions, refusedPermissions); dynamicAssembly = new DynamicAssembly(assemblyBuilder, filename); // TODO: Set entry point and assembly kind. foreach (ModuleDefinition moduleDefinition in assemblyDefinition.Modules) BuildModule(moduleDefinition); metadataPass.Add(delegate { InitializeCustomAttributes(assemblyBuilder.SetCustomAttribute, assemblyDefinition.CustomAttributes); }); }