views:

131

answers:

2

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.

  1. Build the Type X
  2. Define Method GetA on Type X
  3. Create Type X
  4. Call GetA
  5. use same type builder for Type X
  6. Define new Method GetB on Type X
  7. 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

A: 

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.

John Saunders
A: 

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).

Marc Gravell