Click here to Skip to main content
15,893,564 members
Articles / Programming Languages / C#

App.Config Type String Verification with MSBuild

,
Rate me:
Please Sign up or sign in to vote.
4.89/5 (15 votes)
17 Oct 2009Ms-PL8 min read 50.9K   319   28  
How to use an MSBuild custom task to provide compile time verification of string type names in app.config files.
using System;
using System.Collections.Generic;
using System.Linq;

namespace AppConfigVerifier
{
	public class GlobalContext
	{
		readonly List<RuleSetCreator> creators = new List<RuleSetCreator>();
		readonly List<Scanner> scanners = new List<Scanner>();

		public delegate void LogDelegate(GlobalContext sender, String content, 
			Location startLocation, Location endLocation, String message);

		public event LogDelegate ErrorReceived;
		public event LogDelegate WarningReceived;

		public GlobalContext(String content)
		{
			if (content == null)
			{
				throw new ArgumentNullException("content");
			}

			this.content = content;
		}

		public void Run()
		{
			string content = this.content;
			IEnumerable<TypeToken> tokens = scanners.SelectMany(
				scanner => scanner.Scan(content)).Distinct(new TokenComparer());

			var globalInfo = new GlobalInfo(this.content);
			globalInfo.Log.ErrorReceived += globalInfo_ErrorReceived;
			globalInfo.Log.WarningReceived += globalInfo_WarningReceived;
			try
			{
				foreach (TypeToken token in tokens)
				{
					var localContext = new LocalContext(token, globalInfo);
					IEnumerable<Rule> ruleSet = creators.SelectMany(
						creator => creator.CreateRules(localContext));
					foreach (Rule rule in ruleSet)
					{
						rule.Apply(localContext);
					}
				}
			}
			finally
			{
				globalInfo.Log.ErrorReceived -= globalInfo_ErrorReceived;
				globalInfo.Log.WarningReceived -= globalInfo_WarningReceived;
			}
		}


		public void globalInfo_ErrorReceived(GlobalInfo sender, 
			Location location, Location endLocation, string message)
		{
			if (ErrorReceived != null)
			{
				ErrorReceived(this, sender.Content, location, endLocation, message);
			}
		}

		public void globalInfo_WarningReceived(GlobalInfo sender, 
			Location location, Location endLocation, string message)
		{
			if (WarningReceived != null)
			{
				WarningReceived(this, sender.Content, location, endLocation, message);
			}
		}
        
		readonly String content;

		public String Content
		{
			get
			{
				return content;
			}
		}

		public void AddRuleSetCreator(RuleSetCreator creator)
		{
			if (creator == null)
			{
				throw new ArgumentNullException("creator");
			}
			creators.Add(creator);
		}

		public void AddScanner(Scanner scanner)
		{
			if (scanner == null)
			{
				throw new ArgumentNullException("scanner");
			}
			scanners.Add(scanner);
		}
	}
}

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 Microsoft Public License (Ms-PL)


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions