Wednesday, June 01, 2005

Use of Reflection in .NET to create classes at runtime (from scratch)

Just as reflection can be used to retrieve and interpret metadata, it can
be used to construct and "emit" it. The classes found in the System
.Reflection.Emit namespace allow metadata for new types to be generated
in memory and used at runtime. In fact, you can dynamically create
an entire assembly, its classes and methods, and the IL code behind them.
The "in memory" assembly can then be used by other applications.

Good examples and articles can be found at :
http://geekswithblogs.net/johnsPerfBlog/articles/49829.aspx
http://olondono.blogspot.com/2008/02/creating-code-at-runtime.html
http://www.code-magazine.com/Article.aspx?quickid=0301051

Another cool example that reads a text file and compiles and executes the class is available at:
http://vbcity.com/blogs/jatkinson/archive/2010/04/24/dynamically-read-compile-and-run-source-code-from-a-text-file.aspx