using System; using System.Collections.Generic; using System.Linq; using System.Text; using Genuilder.Extensibility; using System.Configuration; namespace StronglyTypedAppSettingsPlugin.Gen { public class StronglyTypedAppSettingsPlugin : IPlugin { #region IPlugin Members public void Initialize(ICodeRepository repository) { repository.CodeItemCreated += new CodeItemCreatedHandler(repository_CodeItemCreated); } void repository_CodeItemCreated(ICodeRepository sender, CodeItem item) { if(item.Name.EndsWith(".config")) { var dependency = item.SourceOf("StronglyTypedSettings.cs"); dependency.ShouldUpdateTarget += new CodeDependencyHandler(dependency_ShouldUpdateTarget); } } void dependency_ShouldUpdateTarget(CodeDependency sender, CodeItem target) { var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = ((FileCodeItem)sender.Source).File.FullName }, ConfigurationUserLevel.None); string generatedClass = "public static class AppConfigSettings{"; foreach(KeyValueConfigurationElement setting in configuration.AppSettings.Settings) { string getter = "public static System.String " + setting.Key + " { get{ return System.Configuration.ConfigurationManager.AppSettings[\"" + setting.Key + "\"]; } }"; generatedClass += getter; } generatedClass += "}"; target.Content = generatedClass; target.Logger.Send("Settings generated !!", Genuilder.Extensibility.Utilities.LogType.Warning); } #endregion } }
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.
This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)