AssemblyBuilder.Save Method (String, PortableExecutableKinds, ImageFileMachine)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Saves this dynamic assembly to disk, specifying the nature of code in the assembly's executables and the target platform.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public void Save(
	string assemblyFileName,
	PortableExecutableKinds portableExecutableKind,
	ImageFileMachine imageFileMachine


Type: System.String

The file name of the assembly.

Type: System.Reflection.PortableExecutableKinds

A bitwise combination of the PortableExecutableKinds values that specifies the nature of the code.

Type: System.Reflection.ImageFileMachine

One of the ImageFileMachine values that specifies the target platform.

Exception Condition

The length of assemblyFileName is 0.


There are two or more modules resource files in the assembly with the same name.


The target directory of the assembly is invalid.


assemblyFileName is not a simple file name (for example, has a directory or drive component), or more than one unmanaged resource, including a version information resources, was defined in this assembly.


The CultureInfo string in AssemblyCultureAttribute is not a valid string and DefineVersionInfoResource was called prior to calling this method.


assemblyFileName is null.


This assembly has been saved before.


This assembly has access RunAssemblyBuilderAccess


An output error occurs during the save.


CreateType has not been called for any of the types in the modules of the assembly to be written to disk.

If imageFileMachine and portableExecutableKind are incompatible, imageFileMachine takes precedence over portableExecutableKind. No exception is thrown. For example, if you specify ImageFileMachine.I386 with PortableExecutableKinds.PE32Plus, PortableExecutableKinds.PE32Plus is ignored.

This method saves all non-transient dynamic modules defined in this dynamic assembly. Transient dynamic modules are not saved. The assembly file name can be the same as the name of one of the module. If so, the assembly manifest is stored within that module. assemblyFileName can be different from the names of all of the modules contained within the assembly. If so, the assembly file contains only the assembly manifest.

For each ResourceWriter obtained using DefineResource, this method writes the .resources file and calls Close to close the stream.

The assemblyFileName needs to be a simple file name without a drive or directory component. To create an assembly in a specific directory, use one of the DefineDynamicAssembly methods that takes a target directory argument.


Write = true or Append = true is needed to save this assembly to the specified file name.


when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.

.NET Framework
Available since 2.0
Return to top