Execution-Time Code Generator for C#


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 exist, 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 manipulating with elements of collections. Request is just string with valid CSharp expression. Code generator generate new class which Apply method returns result of calculation of this expression.  So it can be used as very fast query system which selects objects from the collection according to user specified criteria (very small database engine).It can be also used  if you want to perform some user specified calculation for all members of the collection. Collection can consists of just one element, in this case code generator can be used as very fast calculator. 

Certainly dynamic code generation makes sense only when expression once specified will be executed multiple times or it will be applied to collection with large number of members.

HOW TO USE

This product currently generates two kind of objects: collection filters and mappers. Filter is used to select members of collection matching user supplied criteria. Mapper is used to perform some calculation for all members of the collection and  create new collection with results of such calculation.

Classes MapperGenerator and FilterGenerator are used to generate mappers and filters correspondently. 

Constructor of this class requires type of collection members (it is assumed that al elements of the collection are of the same type or derived from this type) and size of the cache of generated classes. Cache is used to reduce number of generated methods. Generator will first look for 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 Apply method. Result is another collection. In case of filter, it contains elements from the original collection for which specified condition is true. For mappers it contains  results of calculation of specified expression for each element of original collection. Result collection can be converted to array using ToArray method or be iterated using foreach construction.

WHAT CAN BE IN EXPRESSION

Expression can contains any valid CSharp operation with constants and fields of the object (collection element). Type of this object was specified in generator constructor. It is possible to invoke static and instance methods. Currently arrays, unsigned type and unsafe pointers are not supported. Also overloading resolution rules are slightly different from CSharp.

LICENSE

This is public domain product. It is distributed without any warranty, but I will perform e-mail support of it.
You can do with it everything you want except copyrighting it. If you need to use dynamic code generator for some other purposes (except filtering and mapping collection), I will be glad to help you to develop such system.

Up Next
    Ebook Download
    View all
    Learn
    View all