void Save(string assemblyFileName, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine)
{
this.peKind = portableExecutableKind;
this.machine = imageFileMachine;
if ((peKind & PortableExecutableKinds.PE32Plus) != 0 || (peKind & PortableExecutableKinds.Unmanaged32Bit) != 0)
{
throw new NotImplementedException(peKind.ToString());
}
if (machine == ImageFileMachine.IA64 || machine == ImageFileMachine.AMD64)
{
throw new NotImplementedException(machine.ToString());
}
if (resource_writers != null)
{
foreach (IResourceWriter writer in resource_writers)
{
writer.Generate();
writer.Close();
}
}
// Create a main module if not already created
ModuleBuilder mainModule = null;
if (modules != null)
{
foreach (ModuleBuilder module in modules)
{
if (module.FullyQualifiedName == assemblyFileName)
{
mainModule = module;
}
}
}
if (mainModule == null)
{
mainModule = DefineDynamicModule("RefEmit_OnDiskManifestModule", assemblyFileName);
}
if (!is_module_only)
{
mainModule.IsMain = true;
}
/*
* Create a new entry point if the one specified
* by the user is in another module.
*/
if ((entry_point != null) && entry_point.DeclaringType.Module != mainModule)
{
Type[] paramTypes;
if (entry_point.GetParametersCount() == 1)
{
paramTypes = new Type [] { typeof(string) }
}
;
else
{
paramTypes = Type.EmptyTypes;
}
MethodBuilder mb = mainModule.DefineGlobalMethod("__EntryPoint$", MethodAttributes.Static | MethodAttributes.PrivateScope, entry_point.ReturnType, paramTypes);
ILGenerator ilgen = mb.GetILGenerator();
if (paramTypes.Length == 1)
{
ilgen.Emit(OpCodes.Ldarg_0);
}
ilgen.Emit(OpCodes.Tailcall);
ilgen.Emit(OpCodes.Call, entry_point);
ilgen.Emit(OpCodes.Ret);
entry_point = mb;
}
if (version_res != null)
{
DefineVersionInfoResourceImpl(assemblyFileName);
}
if (sn != null)
{
// runtime needs to value to embed it into the assembly
public_key = sn.PublicKey;
}
foreach (ModuleBuilder module in modules)
{
if (module != mainModule)
{
module.Save();
}
}
// Write out the main module at the end, because it needs to
// contain the hash of the other modules
mainModule.Save();
if ((sn != null) && (sn.CanSign))
{
sn.Sign(System.IO.Path.Combine(this.AssemblyDir, assemblyFileName));
}
created = true;
}