using System; using System.Reflection; using System.Reflection.Emit; public class Program { public static void Main() { // define attributes of new type TypeAttributes ta = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass; // create new type builder ModuleBuilder mb = Assembly.GetExecutingAssembly().GetModules()[0] as ModuleBuilder; TypeBuilder tb = mb.DefineType("NewType", ta, typeof(object)); // add method to new type MethodBuilder method = tb.DefineMethod("PrintMessage", MethodAttributes.Public, null, null); ILGenerator gen = method.GetILGenerator(); gen.Emit(OpCodes.Ldstr, "Hello, World!"); gen.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })); gen.Emit(OpCodes.Ret); // create and load new type into assembly Type newType = tb.CreateType(); AssemblyName assemblyName = new AssemblyName("DynamicAssembly"); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule", "NewType.dll"); newType = moduleBuilder.DefineType("NewType", TypeAttributes.Public | TypeAttributes.Class); MethodInfo methodInfo = newType.DefineMethod("PrintMessage", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new Type[] { typeof(string) }); ILGenerator generator = methodInfo.GetILGenerator(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })); generator.Emit(OpCodes.Ret); // save assembly to disk assemblyBuilder.Save("NewType.dll"); // create new instance of dynamic type dynamic obj = Activator.CreateInstance(newType); // call dynamic method obj.PrintMessage(); } }In this example, a new type called "NewType" is dynamically added to the currently executing assembly using the TypeBuilder class. The new type contains a method called "PrintMessage" which writes "Hello, World!" to the console using ILGenerator. The new type is then saved to disk as a separate assembly and a new instance of the type is created and its method is called. The package/library used in this example is the System.Reflection.Emit namespace, which provides classes and interfaces for dynamically generating assemblies, modules, and types at runtime.