Click here to Skip to main content
15,885,065 members
Articles / Programming Languages / C#

Refly, makes the CodeDom'er life easier

Rate me:
Please Sign up or sign in to vote.
4.95/5 (33 votes)
1 Mar 20045 min read 213.2K   1.2K   81  
A smart wrapper around CodeDom that speeds up code generation.
using System;

namespace Refly.Cons
{
	using Refly.CodeDom;
	using Refly.CodeDom.Expressions;
	using Refly.CodeDom.Statements;
	using Refly.Templates;

	/// <summary>
	/// Summary description for SimpleDemo.
	/// </summary>
	public class SimpleDemo
	{
		public static void Output(NamespaceDeclaration ns)
		{
			// output result
			CodeGenerator gen = new CodeGenerator();

			// output to C#
			gen.Provider  = CodeGenerator.CsProvider;
			gen.GenerateCode("./CS",ns);

			// output to VB
			gen.Provider  = CodeGenerator.VbProvider;
			gen.GenerateCode("./VB",ns);
		}

		public static void CreateUser()
		{
			// create namespace
			NamespaceDeclaration ns = new NamespaceDeclaration("MyFirstNamespace");

			// adding imports

			// creating User class
			ClassDeclaration user = ns.AddClass("User");
			
			// put some comments
			user.Doc.Summary.AddText("Refly User class");
			user.Doc.Remarks.Add("para");
			user.Doc.Remarks.Into();
				user.Doc.Remarks.AddText("This class was generated by Refly");
			user.Doc.Remarks.OutOf();

			// adding private fields
			FieldDeclaration name = user.AddField(typeof(string),"name");
			FieldDeclaration lastName = user.AddField(typeof(string),"lastName");

			// adding properties for the fields
			PropertyDeclaration pname = user.AddProperty(name,true,true,true);
			PropertyDeclaration plastName = user.AddProperty(lastName,true,true,true);

			Output(ns);
		}	

		public static void CreateStronglyTypedCollection()
		{
			NamespaceDeclaration ns = new NamespaceDeclaration("Collections");

			CollectionTemplate st = new CollectionTemplate();
			st.ElementType = typeof(string).Name;

			st.Generate(ns);

			Output(ns);
		}

		public static void CreateStronglyTypedDictionary()
		{
			NamespaceDeclaration ns = new NamespaceDeclaration("Collections");

			DictionaryTemplate st = new DictionaryTemplate();
			st.Key = typeof(string).Name;
			st.Value = typeof(int).Name;

			st.Generate(ns);

			Output(ns);
		}

		public static void CreateStronglyTypedDataReader()
		{
			NamespaceDeclaration ns = new NamespaceDeclaration("Collections");

			DataReaderTemplate st = new DataReaderTemplate();
			st.Name = "User";
			st.Data.Fields.Add("name",typeof(string).FullName);
			st.Data.Fields.Add("lastname",typeof(string).FullName);
			st.Generate(ns);

			Output(ns);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions