For automatic code generation in .NET use System.Reflection namespace. This has powerful methods to generate code for various scenarios.