Introduction
If you come from a C++ background, it's likely you have a very "compile-time" view of the world. Because a C++ compiler does all code generation when the code is compiled, C++ programs are static systems that are fully known at compile time.
The Common Language Runtime provides a new way of doing things. The compile-time world still exists, but it is also possible to build dynamic systems where new code is added by loading assemblies or even by writing custom code on the fly.
ABOUT "RUN TIME CODE GENERATOR"
This product provides a very efficient way of handling user requests and manipulating elements of collections. The request is just a string with a valid CSharp expression. The code generator generates a new class, and the Apply method returns the result of the calculation of this expression. So it can be used as a very fast query system that selects objects from the collection according to user-specified criteria (a very small database engine). It can also be used if you want to perform some user-specified calculation for all members of the collection. Collection can consist of just one element; in this case code generator can be used as a very fast calculator.
Certainly, dynamic code generation makes sense only when the expression, once specified, will be executed multiple times, or it will be applied to the collection with a large number of members.
How to use Execution-Time Code Generator?
This product currently generates two kinds of objects: collection filters and mappers. Filter is used to select members of the collection matching user-supplied criteria. The mapper is used to perform some calculations for all members of the collection and create a new collection with the results of such calculations.
Classes MapperGenerator and FilterGenerator are used to generate mappers and filters correspondently.
The constructor of this class requires a type of collection member (it is assumed that all elements of the collection are of the same type or derived from this type) and the size of the cache of generated classes. The cache is used to reduce the number of generated methods. The generator will first look for an expression in the cache, and if it was already generated, the result is reused.
Generate method of MapperGenerator and FilterGenerator class requires string argument with expression and returns mapper of filter. This mapper or filter can be then applied to the particular collection using the Apply method. The result is another collection. In the case of a filter, it contains elements from the original collection for which the specified condition is true. For mappers, it contains the results of the calculation of specified expressions for each element of the original collection. Result collection can be converted to an array using the ToArray method or be iterated using foreach construction.
What can be in Expression?
The expression can contain any valid CSharp operation with constants and fields of the object (collection element). The type of this object was specified in the generator constructor. It is possible to invoke static and instance methods. Currently, arrays, unsigned types, and unsafe pointers are not supported. Also, overloading resolution rules are slightly different from CSharp.
LICENSE
This is a public-domain product. It is distributed without any warranty, but I will perform e-mail support of it. You can do it everything you want except copyright it. If you need to use a dynamic code generator for some other purposes (except filtering and mapping collection), I will be glad to help you to develop such a system.