internal ModuleBuilder CreateModuleBuilder() { AssemblyName name = new AssemblyName(); name.Name = assemblyName; if (options.keyPair != null) { name.KeyPair = options.keyPair; } else if (options.publicKey != null) { name.SetPublicKey(options.publicKey); } name.Version = options.version; assemblyBuilder = StaticCompiler.Universe .DefineDynamicAssembly(name, AssemblyBuilderAccess.ReflectionOnly, assemblyDir); ModuleBuilder moduleBuilder; moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName, assemblyFile, this.EmitDebugInfo); if(this.EmitStackTraceInfo) { AttributeHelper.SetSourceFile(moduleBuilder, null); } if(this.EmitDebugInfo || this.EmitStackTraceInfo) { CustomAttributeBuilder debugAttr = new CustomAttributeBuilder(JVM.Import(typeof(DebuggableAttribute)).GetConstructor(new Type[] { Types.Boolean, Types.Boolean }), new object[] { true, this.EmitDebugInfo }); assemblyBuilder.SetCustomAttribute(debugAttr); } AttributeHelper.SetRuntimeCompatibilityAttribute(assemblyBuilder); if(options.baseAddress != 0) { moduleBuilder.__ImageBase = options.baseAddress; } if(options.fileAlignment != 0) { moduleBuilder.__FileAlignment = options.fileAlignment; } if(options.highentropyva) { moduleBuilder.__DllCharacteristics |= DllCharacteristics.HighEntropyVA; } return moduleBuilder; }
// copied from /mcs/mcs/codegen.cs private void SetPublicKey (AssemblyName an, byte[] strongNameBlob) { // check for possible ECMA key if (strongNameBlob.Length == 16) { // will be rejected if not "the" ECMA key an.SetPublicKey (strongNameBlob); } else { // take it, with or without, a private key RSA rsa = CryptoConvert.FromCapiKeyBlob (strongNameBlob); // and make sure we only feed the public part to Sys.Ref byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob (rsa); // AssemblyName.SetPublicKey requires an additional header byte[] publicKeyHeader = new byte [12] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00 }; byte[] encodedPublicKey = new byte [12 + publickey.Length]; Buffer.BlockCopy (publicKeyHeader, 0, encodedPublicKey, 0, 12); Buffer.BlockCopy (publickey, 0, encodedPublicKey, 12, publickey.Length); an.SetPublicKey (encodedPublicKey); } }