Showing posts with label Generic. Show all posts
Showing posts with label Generic. Show all posts

June 11, 2012

SettingsPropertyValue and SettingsPropertyValueCollection Extensions

I wrote some Configuration Extension, hope you like em ;)
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
        }
    }
}

March 5, 2010

The missing Generic CancelEventHandler<T> Delegate in the BCL

How to make an Generic implementation of an cancel event handler

/// <summary>
    /// Represents the method that handles a cancelable event.
    /// </summary>
    /// <typeparam name="TCancelEventArgs">The type specifier for the event.</typeparam>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">A CancelChangingEventArg&lt;T&gt; that contains the event data.</param>
    [Serializable]
    [System.Security.Permissions.HostProtection(System.Security.Permissions.SecurityAction.LinkDemand, SharedState = true)]
    public delegate void CancelEventHandler<TCancelEventArgs>(object sender, TCancelEventArgs e) where TCancelEventArgs : System.ComponentModel.CancelEventArgs;

    /// <summary>
    /// Provides data for a cancelable changing event.
    /// </summary>
    /// <typeparam name="T">The type of the property that is changing.</typeparam>
    public class CancelChangingEventArg<T> : System.ComponentModel.CancelEventArgs
    {
        private T cv = default(T);
        /// <summary>
        /// Gets the current value of a property as reported by a changing event.
        /// </summary>
        /// <returns>The generic value. In a practical implementation of the CancelChangingEventArg<T>, the generic type of this property is replaced with the constrained type of the implementation.</returns>
        [Description("Current value of a property")]
        public T CurrentValue
        {
            get { return cv; }
        }

        private T nv = default(T);
        /// <summary>
        /// Gets the new value of a property as reported by a changing event.
        /// </summary>
        /// <returns>The generic value. In a practical implementation of the CancelChangingEventArg<T>, the generic type of this property is replaced with the constrained type of the implementation.</returns>
        [Description("New value of a property")]
        public T NewValue
        {
            get { return nv; }
        }

        /// <summary>
        /// Initializes a new instance of the CancelChangingEventArg<T> class, with provided old and new values and the CancelChangingEventArg.Cancel property set to false.
        /// </summary>
        /// <param name="oldValue">Current value of the property at the time of the event.</param>
        /// <param name="newValue">New value of the property.</param>
        public CancelChangingEventArg(T currentValue, T newValue)
            : base()
        {
            cv = currentValue;
            nv = newValue;
        }

        /// <summary>
        /// Initializes a new instance of the CancelChangingEventArg<T> class, with provided old and new values and the CancelChangingEventArg.Cancel property set to the given value.
        /// </summary>
        /// <param name="oldValue">Current value of the property at the time of the event.</param>
        /// <param name="newValue">New value of the property.</param>
        /// <param name="cancel">true to cancel the event; otherwise, false.</param>
        public CancelChangingEventArg(T currentValue, T newValue, bool cancel)
            : base(cancel)
        {
            cv = currentValue;
            nv = newValue;
        }
    }
//Using the code: 
public class MyClass
{
    public event CancelEventHandler<CancelChangingEventArg<int>> SomePropertyChanging;

    private int _SomeProperty;    
    public int SomeProperty    
    {
        get { return _SomeProperty; }
        set { SetProperty(ref _SomeProperty, ref value); }
    }
    
    private void SetProperty(ref int property, ref int value)    
    {
        if (SomePropertyChanging != null)        
        {
            CancelChangingEventArg<int> ResponseArg = new CancelChangingEventArg<int>(property,value);
            SomePropertyChanging(this, ResponseArg);
            if (!ResponseArg.Cancel)
                property = value;        
        }
        else
            property = value;    
    }
    
    public MyClass()    
    {
        _SomeProperty = 10;    
    }

}

public class MyProgram
{
    MyClass myc = new MyClass();
    public MyProgram()    
    {
        myc.SomePropertyChanging += new CancelEventHandler<CancelChangingEventArg<int>>(myc_SomePropertyChanging);
        myc.SomeProperty = 9; //this will be canceled.
        myc.SomeProperty = 11; //this will be accepted, and the value of the SomeProperty is now 11.    
    }
    
    private void myc_SomePropertyChanging(object sender, CancelChangingEventArg<int> e)    
    {
        //here we say if the new value is smaller than the one already in, then cancel the operation.
        //else set the value.
        e.Cancel = (e.CurrentValue > e.NewValue);    
    }
}

Total Pageviews