|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml.XPath;
namespace DiagnosticExplorer
{
public class DiagnosticSectionHandler : IConfigurationSectionHandler
{
private static readonly StringComparer _ignoreCase = StringComparer.CurrentCultureIgnoreCase;
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
DiagnosticConfig config = new DiagnosticConfig();
XPathNavigator navigator = section.CreateNavigator();
config.InstanceName = TryGetValue(navigator.Evaluate("string(instanceName)"), x => x, null);
config.PortMode = TryGetValue(navigator.Evaluate("string(portMode)"), x => (PortMode)Enum.Parse(typeof(PortMode), x), PortMode.Auto);
config.Port = TryGetValue(navigator.Evaluate("string(port)"), int.Parse, 12345);
config.AutoRegister = TryGetValue(navigator.Evaluate("string(autoRegister)"), x => _ignoreCase.Equals(x, bool.TrueString), true);
return config;
}
private static T TryGetValue<T>(object obj, Func<string, T> converter, T def)
{
try
{
if (obj is string == false) return def;
if (obj == null) return def;
return converter(obj as string);
}
catch (Exception ex)
{
return def;
}
}
}
}
|
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.
I am a software developer originally from Auckland, New Zealand. I have lived and worked in London since 2005.