Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

CodeDom Assistant

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
21 Sep 20074 min read 138.8K   6.6K   82  
Generating CodeDom Code By Parsing C# or VB
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Windows.Forms;

namespace ScintillaNet
{
	public class WhiteSpaceStringConverter : TypeConverter
	{
		private static readonly Regex rr = new Regex("\\{0x([0123456789abcdef]{1,4})\\}", RegexOptions.IgnoreCase);

		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			if (sourceType == typeof(char) || sourceType == typeof(string))
				return true;

			return false;
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			if (destinationType == typeof(char) || destinationType == typeof(string))
				return true;

			return false;
		}

		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			string val = convertFrom(value.ToString());


			if (context.PropertyDescriptor.ComponentType == typeof(char))
				return val[0];

			return val;
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			string val = convertTo(value.ToString());

			if (destinationType == typeof(char))
				return val[0];

			return val;
		}

		public override bool IsValid(ITypeDescriptorContext context, object value)
		{
			return true;
		}


		private string convertTo(string nativeString)
		{
			StringBuilder sb = new StringBuilder();
			foreach (char c in nativeString)
			{
				if ((int)c > 32)
				{
					sb.Append(c);
					continue;
				}

				switch ((int)c)
				{
					case 9:
						sb.Append("{TAB}");
						break;
					case 10:
						sb.Append("{LF}");
						break;
					case 13:
						sb.Append("{CR}");
						break;
					case 32:
						sb.Append("{SPACE}");
						break;
					default:
						sb.Append("{0x" + ((int)c).ToString("x4") + "}");
						break;
				}
			}
			return sb.ToString();
		}


		private string convertFrom(string value)
		{
			Match m = rr.Match(value);
			while (m.Success)
			{
				int val = int.Parse(m.Value.Substring(3, m.Length - 4), NumberStyles.AllowHexSpecifier);
				value = value.Replace(m.Value, ((char)val).ToString());
				m = rr.Match(value);
			}
			value = value.Replace("{TAB}", "\t").Replace("{LF}", "\r").Replace("{CR}", "\n").Replace("{SPACE}", " ");

			return value;
		}


	}
}

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
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions