using System.Configuration; using System; namespace PawJershauge.Extensions { namespace Configuration { /// <summary> /// Provides Extension for the Configuration manager /// </summary> public static class ConfigurationExtensions { #region SettingsPropertyValue /// <summary> /// Get the string value of the property /// </summary> /// <param name="obj">SettingsPropertyValue object to extend the method onto.</param> /// <returns>null if value is null; else the string representing tha value.</returns> /// <remarks>Developed by Paw Jershauge (Check my Blog fore more C# tips: [C# and I] Url: http://pawjershauge.blogspot.com)</remarks> public static string GetValue(this SettingsPropertyValue obj) { return obj.PropertyValue == null ? null : obj.PropertyValue.ToString(); } /// <summary> /// Get the T value of the property /// </summary> /// <typeparam name="T">Generic type to get as</typeparam> /// <param name="obj">SettingsPropertyValue object to extend the method onto.</param> /// <returns>The T value of the property, if the value is null, then default(T) is returned.</returns> /// <remarks>Developed by Paw Jershauge (Check my Blog fore more C# tips: [C# and I] Url: http://pawjershauge.blogspot.com)</remarks> public static T GetValue<T>(this SettingsPropertyValue obj) where T : struct { if (obj.PropertyValue == null) return default(T); else return (T)Convert.ChangeType(obj.PropertyValue, typeof(T)); } /// <summary> /// Tries to get the T value of the property. /// </summary> /// <typeparam name="T">Generic type to get as</typeparam> /// <param name="obj">SettingsPropertyValue object to extend the method onto.</param> /// <param name="value">if return value is true, the out value parameter has the T value of the property</param> /// <returns>True if the value is not null, else False.</returns> /// <remarks>Developed by Paw Jershauge (Check my Blog fore more C# tips: [C# and I] Url: http://pawjershauge.blogspot.com)</remarks> public static bool TryGetValue<T>(this SettingsPropertyValue obj, out T value) where T : struct { if (obj.PropertyValue == null) { value = default(T); return false; } else { value = (T)Convert.ChangeType(obj.PropertyValue, typeof(T)); return true; } } #endregion #region SettingsPropertyValueCollection /// <summary> /// Checks if the collection contains the key. /// </summary> /// <param name="obj">SettingsPropertyValueCollection object to extend the method onto.</param> /// <param name="name">Name of the key to lookup</param> /// <returns>true if the key exists else false.</returns> /// <remarks>Developed by Paw Jershauge (Check my Blog fore more C# tips: [C# and I] Url: http://pawjershauge.blogspot.com)</remarks> public static bool ContainsKey(this SettingsPropertyValueCollection obj, string name) { if (obj == null || obj.Count == 0) return false; foreach (SettingsPropertyValue item in obj) { if (item.Name == name) return true; } return false; } /// <summary> /// Get the string value of the property /// </summary> /// <param name="obj">SettingsPropertyValueCollection object to extend the method onto.</param> /// <returns>null if value is null; else the string representing tha value.</returns> /// <remarks>Developed by Paw Jershauge (Check my Blog fore more C# tips: [C# and I] Url: http://pawjershauge.blogspot.com)</remarks> public static string GetValue(this SettingsPropertyValueCollection obj, string name) { if (!obj.ContainsKey(name)) return null; return obj[name].GetValue(); } /// <summary> /// Get the T value of the property /// </summary> /// <typeparam name="T">Generic type to get as</typeparam> /// <param name="obj">SettingsPropertyValueCollection object to extend the method onto.</param> /// <returns>The T value of the property, if the value is null, then default(T) is returned.</returns> /// <remarks>Developed by Paw Jershauge (Check my Blog fore more C# tips: [C# and I] Url: http://pawjershauge.blogspot.com)</remarks> public static T GetValue<T>(this SettingsPropertyValueCollection obj, string name) where T : struct { if (!obj.ContainsKey(name)) return default(T); return obj[name].GetValue<T>(); } /// <summary> /// Tries to get the T value of the property. /// </summary> /// <typeparam name="T">Generic type to get as</typeparam> /// <param name="obj">SettingsPropertyValueCollection object to extend the method onto.</param> /// <param name="value">if return value is true, the out value parameter has the T value of the property</param> /// <returns>True if the value is not null, else False.</returns> /// <remarks>Developed by Paw Jershauge (Check my Blog fore more C# tips: [C# and I] Url: http://pawjershauge.blogspot.com)</remarks> public static bool TryGetValue<T>(this SettingsPropertyValueCollection obj, string name, out T value) where T : struct { if (!obj.ContainsKey(name)) { value = default(T); return false; } return obj[name].TryGetValue(out value); } #endregion } } }
June 11, 2012
SettingsPropertyValue and SettingsPropertyValueCollection Extensions
I wrote some Configuration Extension, hope you like em ;)
Etiketter:
.Net Framework,
C#,
Extension,
Framework 3.5,
Generic
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment