Modifying a type after it has been created with TypeBuilder.CreateType
-
22-08-2019 - |
Question
I'm creating a dynamic class in a dynamic assembly at runtime, and want to be able to Define methods for that class on demand.
Ex.
- Build the Type X
- Define Method GetA on Type X
- Create Type X
- Call GetA
- use same type builder for Type X
- Define new Method GetB on Type X
- Recreate type X, that now has both GetA and GetB
if that is impossible then I will have to build the type on demand for one of the methods, and to do that then I have to create the assembly as well, on each time.
Any Ideas, Thanks
No correct solution
OTHER TIPS
You can't create "full" methods (discoverable via reflection) - however, you can use DynamicMethod
to associate new IL blocks with a type (primarily for scope etc). But ultimately, what you describe is a job for the DLR in 4.0 (and a language such as Iron Python; C# and VB don't add language support for declaring dynamic methods - only consuming them).
Perhaps you should create the type with CodeDom. You can build the entire type, compile the code, then load the new assembly and use the type.