AssemblyName assemblyName = new AssemblyName("DynamicAssembly"); AppDomain currentDomain = AppDomain.CurrentDomain; AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule"); TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicClass"); typeBuilder.DefineField("MyField", typeof(string), FieldAttributes.Public); Type myType = typeBuilder.CreateType(); Console.WriteLine(myType.Name);
AssemblyName assemblyName = new AssemblyName("DynamicAssembly"); AppDomain currentDomain = AppDomain.CurrentDomain; AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule"); TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicInterface", TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public); typeBuilder.DefineMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract); Type myType = typeBuilder.CreateType(); Console.WriteLine(myType.Name);In this example, we create a new assembly and a new module. Then we create a new interface using the DefineType method with TypeAttributes.Interface, TypeAttributes.Abstract, and TypeAttributes.Public flags. The interface has a public abstract method named MyMethod. Finally, we create the type using the CreateType method and print the type's name.