Sams .NET Stuff : All my .NET stuff
Updated: 8/5/2002; 11:07:24 PM.

 

Categories:
Personal Friends:

Subscribe to "Sams .NET Stuff" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

Friday, March 29, 2002

I am also playing around with System.CodeDom. What an awesome
namespace! Using classes like System.CodeDom.CodeStatementCollection
and System.CodeDom.Compiler.ICodeGenerator, you can actually
generate both C# and VB.NET code on the fly. Take a look at
this code to generate i = 10 in both languages:
using System;
using System.CodeDom;

namespace CodeDomBaby
{
///
/// Summary description for Class1.
///

class Class1
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
System.CodeDom.CodeStatementCollection statements = new CodeStatementCollection();
 CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement
(System.Type.GetType("System.Int32"), "i", new CodePrimitiveExpression(10));

CodeVariableReferenceExpression variableRef =
new CodeVariableReferenceExpression("i");

statements.Add(variable);

System.CodeDom.Compiler.CodeDomProvider provider;
bool generateCSharp = true;

if (generateCSharp)
provider = new Microsoft.CSharp.CSharpCodeProvider();
else
provider = new Microsoft.VisualBasic.VBCodeProvider();

System.CodeDom.Compiler.ICodeGenerator generator = provider.CreateGenerator();

foreach (CodeStatement s in statements)
generator.GenerateCodeFromStatement(s, Console.Out,
new System.CodeDom.Compiler.CodeGeneratorOptions());


}
}
}



9:28:37 PM    


© Copyright 2002 Sam Gentile.



Click here to visit the Radio UserLand website.

 


March 2002
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Feb   Apr