Example #1
0
        /// <summary>
        /// Sets up cached PropertyInfo and determines the best delegate to use to compare values
        /// retrieved from that property.
        /// </summary>
        public void Initialize()
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(AppSettings.Culture);

            if (fFoundProperty == false)
            {
                fFoundProperty = true;
                if (pi == null)
                {
                    PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
                    property = props[sPropertyName];
                    pi       = typeof(T).GetProperty(sPropertyName);

                    if (pi == null)
                    {
                        throw new Exception(Properties.Resources.ValueCompare_PropertyName + " " + sPropertyName +
                                            " " + Properties.Resources.ValueCompare_NotFound + " " + typeof(T).Name);
                    }
                }
                typ = pi.PropertyType;
                // Set up the property comparison delegate to use based on the type of values we will be comparing
                if (sortType == SortType.eUsePropertyOrFieldType)
                {
                    sortType = Sorting.GetSortTypeEnumForType(typ);
                    if (typ == typeof(string))
                    {
                        if (stringComparisonToUse == StringComparison.Ordinal)
                        {
                            DoCompare = StringCompareOrdinal;
                        }
                        else
                        {
                            DoCompare = StringCompare;
                        }
                    }
                    else if (typ == typeof(int) && !fSortDescending)
                    {
                        DoCompare = CompareInt;
                    }
                    else if (typ == typeof(int))
                    {
                        DoCompare = CompareIntDesc;
                    }
                    else if (typ == typeof(DateTime))
                    {
                        DoCompare = CompareDates;
                    }
                    else if (typ == typeof(long))
                    {
                        DoCompare = CompareTypeSensitive <long>;
                    }
                    else if (typ == typeof(double))
                    {
                        DoCompare = CompareTypeSensitive <double>;
                    }
                    else if (typ == typeof(float))
                    {
                        DoCompare = CompareTypeSensitive <float>;
                    }
                    else if (typ == typeof(short))
                    {
                        DoCompare = CompareTypeSensitive <short>;
                    }
                    else if (typ == typeof(byte))
                    {
                        DoCompare = CompareTypeSensitive <byte>;
                    }
                    else if (typ == typeof(bool))
                    {
                        DoCompare = CompareTypeSensitive <bool>;
                    }
                    else if (typ.BaseType == typeof(Enum))
                    {
                        FastEnumLookup = new Dictionary <int, string>(32);
                        if (fSortDescending)
                        {
                            DoCompare = FastCompareEnumsDesc;
                        }
                        else
                        {
                            DoCompare = FastCompareEnumsAsc;
                        }
                    }
                    else
                    {
                        DoCompare = CompareUsingToString;
                    }
                }
                else
                {
                    if (sortType == SortType.eString)
                    {
                        DoCompare = CompareUsingToString;
                    }
                    else if (sortType == SortType.eByte)
                    {
                        DoCompare = CompareUsingToByte;
                    }
                    else if (sortType == SortType.eDateTime)
                    {
                        DoCompare = CompareUsingToDate;
                    }
                    else if (sortType == SortType.eInteger)
                    {
                        DoCompare = CompareUsingToInt;
                    }
                    else if (sortType == SortType.eLong)
                    {
                        DoCompare = CompareUsingToInt64;
                    }
                    else if (sortType == SortType.eDoubleOrFloat)
                    {
                        DoCompare = CompareUsingToDouble;
                    }
                    else
                    {
                        DoCompare = CompareUsingToString;
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Sets up cached FieldInfo and determines the best delegate to use to compare values
        /// retrieved from that field.
        /// </summary>
        public void Initialize()
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(AppSettings.Culture);

            if (fFoundField == false)
            {
                fFoundField = true;

                if (fi == null)
                {
                    // You can play around with binding flags if you really want to access nonpublic fields, etc...
                    // note that there is a significant performance hit on accessing protected and private fields,
                    // since security / permissions are checked every time, from what I can tell.  It's better
                    // just to go through public properties if you're not accessing public fields.
                    // fi = typeof(T).GetField(sFieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    fi = typeof(T).GetField(sFieldName);
                    if (fi == null)
                    {
                        throw new Exception(Properties.Resources.ValueCompare_FieldName + " " + sFieldName +
                                            " " + Properties.Resources.ValueCompare_NotFound + " " + typeof(T).Name);
                    }
                }
                typ = fi.FieldType;
                if (sortType == SortType.eUsePropertyOrFieldType)
                {
                    sortType = Sorting.GetSortTypeEnumForType(typ);
                    if (typ == typeof(string))
                    {
                        if (stringComparisonToUse == StringComparison.Ordinal)
                        {
                            DoCompare = StringCompareOrdinal;
                        }
                        else
                        {
                            DoCompare = StringCompare;
                        }
                    }
                    else if (typ == typeof(int) && !fSortDescending)
                    {
                        DoCompare = CompareInt;
                    }
                    else if (typ == typeof(int))
                    {
                        DoCompare = CompareIntDesc;
                    }
                    else if (typ == typeof(DateTime))
                    {
                        DoCompare = CompareDates;
                    }
                    else if (typ == typeof(long))
                    {
                        DoCompare = CompareTypeSensitive <long>;
                    }
                    else if (typ == typeof(double))
                    {
                        DoCompare = CompareTypeSensitive <double>;
                    }
                    else if (typ == typeof(float))
                    {
                        DoCompare = CompareTypeSensitive <float>;
                    }
                    else if (typ == typeof(short))
                    {
                        DoCompare = CompareTypeSensitive <short>;
                    }
                    else if (typ == typeof(byte))
                    {
                        DoCompare = CompareTypeSensitive <byte>;
                    }
                    else if (typ == typeof(bool))
                    {
                        DoCompare = CompareTypeSensitive <bool>;
                    }
                    else if (typ.BaseType == typeof(Enum))
                    {
                        FastEnumLookup = new Dictionary <int, string>(32);
                        if (fSortDescending)
                        {
                            DoCompare = FastCompareEnumsDesc;
                        }
                        else
                        {
                            DoCompare = FastCompareEnumsAsc;
                        }
                    }
                    else
                    {
                        DoCompare = CompareUsingToString;
                    }
                    // optimize to use the ABOVE path if the property or field type matches
                    // the requested sort type (i.e. below)
                }
                else
                {
                    if (sortType == SortType.eString)
                    {
                        DoCompare = CompareUsingToString;
                    }
                    else if (sortType == SortType.eByte)
                    {
                        DoCompare = CompareUsingToByte;
                    }
                    else if (sortType == SortType.eDateTime)
                    {
                        DoCompare = CompareUsingToDate;
                    }
                    else if (sortType == SortType.eInteger)
                    {
                        DoCompare = CompareUsingToInt;
                    }
                    else if (sortType == SortType.eLong)
                    {
                        DoCompare = CompareUsingToInt64;
                    }
                    else if (sortType == SortType.eDoubleOrFloat)
                    {
                        DoCompare = CompareUsingToDouble;
                    }
                    else
                    {
                        DoCompare = CompareUsingToString;
                    }
                }
            }
        }