Ejemplo n.º 1
0
        /// <summary>
        /// Evaluates the minimum value
        /// </summary>
        /// <param name="t">Generic type</param>
        /// <param name="dataSource">The data source.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public object EvaluateMax <T>(T t, object dataSource, string propertyName)
        {
            QueryParser      parser    = null;
            ObjectReflection reflector = null;

            //check the subtype now
            object value;
            Type   tp;

            try
            {
                parser    = new QueryParser();
                reflector = new ObjectReflection();

                T typeInstance = this.GetTypeInstance <T>(dataSource);

                //get the type of the field
                reflector.GetTypeOfField(typeInstance, propertyName, out tp, out value);

                if (parser.IsNumeric(tp))
                {
                    decimal resultValue = this.EvaluateNumeric <T>(dataSource, propertyName, false);
                    return(resultValue);
                }
                else if (parser.IsDateTime(tp))
                {
                    DateTime resultDate = this.EvaluateDateTime <T>(dataSource, propertyName, false);
                    return(resultDate);
                }
                else
                {
                    //quick check to see if out type supports IComparable.
                    //Because we have a RuntimeType we cannot cast it to Icomparable
                    //so we check it with reflection

                    Type icTest = tp.GetInterface("System.IComparable");

                    if (icTest == null)
                    {
                        throw new ArgumentException("Invalid type. Must implement IComparable to be evaluated");
                    }

                    return(this.EvaluateGeneric <T>(dataSource, propertyName, false));
                }
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// </summary>
        /// <param name="dataSource"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public decimal Sum <T>(object dataSource, string fieldName)
        {
            ObjectQueryEvaluator objectEvaluator = null;
            QueryParser          parser          = null;
            ObjectReflection     reflector       = null;

            decimal result = 0;

            try
            {
                //check the subtype now
                object value;
                Type   tp;

                //it's generic object
                objectEvaluator = new ObjectQueryEvaluator();
                parser          = new QueryParser();
                reflector       = new ObjectReflection();

                T typeInstance = this.GetTypeInstance <T>(dataSource);

                reflector.GetTypeOfField(typeInstance, fieldName, out tp, out value);

                if (!parser.IsNumeric(tp))
                {
                    throw new ArgumentException("Invalid type for property. Only numeric fields are accepted.");
                }
                else
                {
                    IEnumerator ienum = ((IEnumerable)dataSource).GetEnumerator();

                    while (ienum.MoveNext())
                    {
                        reflector.GetTypeOfField((T)ienum.Current, fieldName, out tp, out value);

                        //add it to the result
                        result += Convert.ToDecimal(value);
                    }
                }

                return(result);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a new instance of ObjectQueryEvaluator
 /// </summary>
 public ObjectQueryEvaluator()
 {
     cachePropertyTypes = new Dictionary <string, int>();
     cacheFieldsTypes   = new Dictionary <string, int>();
     reflector          = new ObjectReflection(ref cachePropertyTypes, ref cacheFieldsTypes);
 }
Ejemplo n.º 4
0
        public T EvaluateGeneric <T>(object dataSource,
                                     string propertyName,
                                     bool evaluateMinimum)
        {
            ObjectReflection reflector = null;
            object           current   = null;
            int i             = 0;  //index counter
            int selectedIndex = -1; //selected index

            try
            {
                reflector = new ObjectReflection();

                Type   tp    = null;
                object value = null;

                IEnumerator ienum = ((IEnumerable)dataSource).GetEnumerator();

                while (ienum.MoveNext())
                {
                    T instance = (T)ienum.Current;

                    reflector.GetTypeOfField <T>(instance, propertyName, out tp, out value);

                    if (i == 0)
                    {
                        current       = value;
                        selectedIndex = i;
                        ++i;
                        continue;
                    }

                    int result = ((IComparable)value).CompareTo(current);

                    if (evaluateMinimum)
                    {
                        if (result == -1)
                        {
                            current       = value;
                            selectedIndex = i;
                            ++i;
                        }
                    }
                    else
                    {
                        if (result > 0)
                        {
                            current       = value;
                            selectedIndex = i;
                            ++i;
                        }
                    }
                }

                //get the item at the selected index
                ienum = ((IEnumerable)dataSource).GetEnumerator();
                i     = -1;

                while (ienum.MoveNext())
                {
                    ++i;

                    if (i == selectedIndex)
                    {
                        current = ienum.Current;
                        break;
                    }
                }


                return((T)current);
            }
            catch
            {
                throw;
            }
        }