.NET : .NET News
Updated: 7/1/2002; 9:28:23 PM.

 

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

Playing Around with .NET's System.CodeDom classes


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

Click on the coffee mug to add Sam Gentile's Instant Outline to your Radio UserLand buddy list.