Code generation

  • Thread starter Thread starter Alexander Muylaert
  • Start date Start date
A

Alexander Muylaert

Hi

I'm trying optimize certain pieces of my code by generating and compiling
the source at runtime.

I need to make 2 methods for each class, a read and a write. I would like
to do this through generated code because than I don't need to use the
reflection all the time, nor do I need to box and unbox each item.

I would like to have a lazy approach here, I would like to generate and
compile the source as late as possible. I don't mind to compile the read
and write function at the same time.

Is it ok if I would make a seperate Assembly for each class? I would end up
with a couple of hundred tiny assemblies in my application then.

I have found examples on how to do this, but how can I "append" 2 methods to
an already existing in memory assembly? Without the need to recompile
everything. I would like to compile only those 2 methods or the class
containing these two methods.

kind regards

Alexander
 
You actually don't need to save the assembly to disk. You can use the
TypeBuilder and AssemblyBuilder classes of the System.Reflection.Emit
namespace to build them in memory and use it directly without saving it to
disc.
 
Brian Delahunty said:
You actually don't need to save the assembly to disk. You can use the
TypeBuilder and AssemblyBuilder classes of the System.Reflection.Emit
namespace to build them in memory and use it directly without saving it to
disc.

Unless there's an absolute need to create instance methods in separate
classes, it is more convenient to use the newly added
System.Reflection.Emit.DynamicMethod to create static methods. No
need for creating assemblies, classes, and so on.

That produces an object that can be cast to a delegate type and called
as a delegate without any boxing or unboxing.

Peter
 
Back
Top