Emitting classes with TypeBuilder [Answered]

The idea is to have certain primitive properties bound to a type through an attribute. The user of the library can create the type and bind it or use a different attribute that creates the type automatically and adds the previously mentioned attribute with that new type. I know it's much slower, but it would be a good option to have for simple prototyping. So I found TypeBuilder.CreateType https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.typebuilder.createtype?view=net-6.0 but the method isn't static and TypeBuilder has no constructors. The example in the docs gets an instance through some other objects and it all starts with AppDomain.DefineDynamicAssembly which for some reason I don't have despite being on .net 6 with all the same usings as in the example.
TypeBuilder.CreateType Method (System.Reflection.Emit)
Creates a Type object for the class. After defining fields and methods on the class, CreateType is called in order to load its Type object.
2 Replies
TheBoxyBear
TheBoxyBear2y ago
Seems CreateType is now DefineType in ModuleBuilder but I'm also stuck getting an instance of that The example in the ModuleBuilder docs also relies on DefineDynamicAssembly Found it: AssemblyBuilder.DefineDynamicAssembly (static)
Accord
Accord2y ago
✅ This post has been marked as answered!