This page has been translated using an automatic translation service and the translation may not be very accurate.

Lists of tipizzate and ordinabili objects (English)

If you use this code and feel it is a useful tool, consider making a donation (through PayPal) to help support the project. You can donate as little or as much as you wish, any amount is greatly appreciated!

Italian page For the original Italian page, please click on the link: http://guru4.net/articoli/sortable-collectionbase/default.aspx

To create lists of strongly tipizzate and ordinabili objects on the base of a property of the contained entity in the same list is a necessity much frequent one when they are used custom entities (business objects). In this article possible implementazione, sufficiently generic comes proposed one and simple.

The base classes

In order to simplify the creation of collection tipizzate framework us it puts to disposition the abstract class System.Collections.CollectionBase: deriving from this an other abstract class (SortableCollectionBase) we can add the possibility to order the list, exposing (and implementing!) the method Sort. In order to order generic objects on the base of one given property, the ObjectComparer class will use reflection, therefore not to have to specialize the criteria of comparison for every detailed list entity.
For a greater understanding, the class is looked at diagram following:

SortableCollectionBase - Class diagram

SortableCollectionBase

The abstract class SortableCollectionBase, will be the point of departure for the generation of tipizzate lists:

using System;
using System.Collections;

namespace G4N.Common
{
    public abstract class SortableCollectionBase : CollectionBase
    {
        public void Sort(string propertyname)
        {
            this.Sort(propertyname, true);
        }
        
        public void Sort(string propertyname, bool ascending)
        {
            base.InnerList.Sort(new ObjectComparer(propertyname, ascending));
        }
    }
}

As we can see, to the class base CollectionBase it comes added the method simply Sort (subordinate to overload for the definition of the ordering criterion). The implementazione of the method is common to all the lists and takes advantage of the class ObjectComparer described in the next paragraph.

ObjectComparer

The ObjectComparer class implements, using reflection, the generic comparison between objects on the base of one they property:

using System;
using System.Collections;
using System.Reflection;

namespace G4N.Common
{
    public class ObjectComparer : IComparer
    {
        private string m_propertyname;
        private bool m_ascending;

        public ObjectComparer(string propertyname, bool ascending)
        {
            m_propertyname = propertyname;
            m_ascending = ascending;
        }
        
        public int Compare(object x, object y)
        {
            Type tx = x.GetType();
            Type ty = y.GetType();

            PropertyInfo px = tx.GetProperty(m_propertyname);
            PropertyInfo py = ty.GetProperty(m_propertyname);

            IComparable vx = (IComparable)px.GetValue(x, null);
            object vy = py.GetValue(y, null);

            int result = vx.CompareTo(vy);
            if (result != 0)
            {
                if (m_ascending)
                    return result;
                else
                    return -result;
            }
            return 0;
        }
    }
}

To create one tipizzata and ordinabile list

Once that we have defined the base classes, we pass to the implementazione of an example class and analyze of it the code:

using System;
using G4N.Common;

namespace Demo
{
    [Serializable]
    public class UserList : SortableCollectionBase
    {

        public UserList() { }

        public int Add(User value)
        {
            return base.List.Add(value as object);
        }

        public User this[int index]
        {
            get { return (base.List[index] as User); }
        }

        public void Remove(User value)
        {
            base.List.Remove(value as object);
        }

        public void Insert(int index, User value)
        {
            base.List.Insert(index, value as object);
        }

        public bool Contains(User value)
        {
            return base.List.Contains(value as object);
        }

    }
}

The implementazione of UserList (ordinabile list of objects of type "User", hypothetical class, omitted in the example) has simply specialized the main methods (reading, iterance, adding, removal and verification of the contained elements) of access to the list base (base.List), forcing of the type to User

In analogous way it is possible to define every other type of custom collection, simply modifying evidenced how much in grassetto in the previous code, that is:

  • specifying the namespace in which inserting the class list (in the example: "Demo")

  • defining the name of the class list (in the example: "UserList")

  • modifying the type of entity managed (in the example: "User")

Using the technology "Snippet Tails" of the IDE Microsoft it is possible to automate the process of generation of classes custom that they derive give SortableCollectionBase. Attached to this article found, beyond to the code source, the Snippet Tails in order to generate the classes without having to write one code line.
For greater information on the technology Snippet Tails Introduction to the technology is sent back to the article "Snippet Tails".