How to create RunAndSave Assembly using System.Reflection.Emit in .NET 6?

Nicholas Piazza 531 Reputation points
2021-12-24T20:47:15.937+00:00

In the .NET 6 documentation for the AssemblyBuilder class in System.Reflection.Emit there is an example of how to build an assembly. The example can be made to compile and work in .NET 6 by changing

AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly 
                                      (aName, AssemblyBuilderAccess.RunAndSave);

to
AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly
(aName, AssemblyBuilderAccess.Run);
and by commenting out

 ab.Save(aName.Name + ".dll");

The example will run but you cannot save the created assembly becausethe DefineDynamicAssembly() method does not exist in the .NET 6 version of System.AppDomain, the RunAndSave field of AssemblyBuilderAccess does not exist in the .NET 6 version of System.Reflection.Emit, and the Save() method of AssemblyBuilder does not exist in the .NET version of System.Reflection.Emit. Those three items are only in the .NET Framework 4.8 version.

Is there a way to save the created assembly in .NET 6?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,266 questions
{count} votes