Click here to Skip to main content
12,443,996 members (43,101 online)
Click here to Skip to main content

Stats

20.8K views
287 downloads
42 bookmarked
Posted

Document and Code Generation by LINQ and XSL

, 3 Aug 2008 CPOL
An article on how to generate source code as well as populate Excel Spreadsheets.
using System;
using System.Text;

namespace LinqCodeGeneration
{
	public class TestCode
	{
		/// <summary>
		/// 
		/// </summary>
		public static void Print()
		{
			Console.Out.WriteLine("\n\n");
			Console.Out.WriteLine("=========================");
			Console.Out.WriteLine("    Test Code");
			Console.Out.WriteLine("=========================");
			Console.Out.WriteLine();

			Console.Out.WriteLine(CreateProperty("varchar", "lastName"));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fieldType"></param>
		/// <param name="fieldName"></param>
		/// <returns></returns>
		public static string CreateProperty(string fieldType, string fieldName)
		{
			StringBuilder sb = new StringBuilder();

			sb.Append("\n");
			sb.Append("		/// <summary>\n");
			sb.Append("		/// Get/Set for " + PropertyName(fieldName) + "\n");
			sb.Append("		/// </summary>\n");
			sb.Append("		public " + FieldType(fieldType) + " " + PropertyName(fieldName) + "\n");
			sb.Append("		{\n");
			sb.Append("		    get { return (" + FieldName(fieldName) + "); }\n");
			sb.Append("		    set { " + FieldName(fieldName) + " = value; }\n");
			sb.Append("		}\n");

			return (sb.ToString());
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fieldtype"></param>
		/// <returns></returns>
		public static string FieldType(string fieldtype)
		{
			switch (fieldtype.ToLower())
			{
				case "smallint":
					return "short";
				case "uniqueidentifier":
					return "Guid";
				case "varchar":
					return "string";
				default:
					return fieldtype;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fieldtype"></param>
		/// <returns></returns>
		public static string FieldName(string fieldtype)
		{
			int index;

			StringBuilder sb = new StringBuilder(fieldtype);

			for (index = 0; index < sb.Length; index++)
			{
				if (char.IsUpper(sb[index]))
				{
					sb[index] = Char.ToLower(sb[index]);
				}
				else if (char.IsLower(sb[index]))
				{
					if (index > 0)
					{
						sb[index - 1] = Char.ToUpper(sb[index - 1]);
					}

					break;
				}
			}

			return "_" + sb;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fieldname"></param>
		/// <returns></returns>
		public static string PropertyName(string fieldname)
		{
			string propertyName = FieldName(fieldname);
			propertyName = Char.ToUpper(propertyName[1]) + propertyName.Substring(2);

			return propertyName;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public string InsertText(string text)
		{
			return (text);
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dave Elliott
Software Developer (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 3 Aug 2008
Article Copyright 2008 by Dave Elliott
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid