Get it and Do something with it.

I wrote a much better version of the Get & Do that I’m happy with.  Here’s the code below.

// Using this:

private void Form1_Load(object sender, EventArgs e)

{

    new Get(this.button1, this.button2, this.button3, this.button4)

        .Do(delegate(Control c) { c.Enabled = false; return c; })

        .Do(delegate(Control c) { c.BackColor = Color.Red; return c; });

    new Get(this.Controls)

        .Do(delegate(Control c) { c.Visible = false; return c; });

}

// The Get class below.

using System.Collections;
using System.Collections.Generic;

namespace GetAndDo
{
    ///


    /// Get something we want
    ///

    ///
    class Get
    {
        public List objects;

        ///


        /// Create a Get wrapper around something we want
        ///

        /// param array of stuff
        ///
        /// Try to design the constructor to be functional and readable.
        ///

        public Get(params object[] getObjs)
        {
            objects = new List();
            foreach (object obj in getObjs)
            {
                if (obj is IEnumerable)
                {
                    objects.AddRange((IEnumerable)obj);
                    continue;
                }
                if (obj is IEnumerable)
                {
                    foreach (object obj1 in (IEnumerable)obj)
                    {
                        if (obj1 is T)
                        {
                            objects.Add((T)obj1);
                        }
                    }
                }
                if (obj is T)
                {
                    objects.Add((T)obj);
                }
            }
        }

        ///


        /// Signature of a delegate for any actions
        ///

        ///
        ///
        /// null - filter from result set (filter-operation)
        /// true - include obj in result set
        /// T - include new object in result set (map-operation)
        /// IEnumerable - useful for nesting deep
        ///

        public delegate object Action(T obj);

        ///


        /// Run delegate on each element in the current Get wrapper
        ///

        /// delegate to run
        ///
        /// New Get wrapper - does not modify original wrapper.
        /// This allows chaining of delegates
        ///

        public Get Do(Action action)
        {
            List newObjects = new List();
            for (int i = 0; i < objects.Count; i++)
            {
                object result = action(objects[i]);
                if (result == null)
                {
                    continue;
                }
                if (result is T)
                {
                    newObjects.Add((T)result);
                    continue;
                }
                if (result is IEnumerable)
                {
                    newObjects.AddRange((IEnumerable)result);
                }
                if (result is IEnumerable)
                {
                    foreach (object obj1 in (IEnumerable)result)
                    {
                        if (obj1 is T)
                        {
                            objects.Add((T)obj1);
                        }
                    }
                }
                if (true.Equals(result))
                {
                    newObjects.Add(objects[i]);
                    continue;
                }
            }
            return new Get(newObjects.ToArray());
        }
    }
}

Discussions