Ejemplo n.º 1
0
        ////////////////////////////////////////////////////////////////////////

        public static Object Execute(DynContext EvaluateContext, Object obj, IList <Object> Parameters)
        {
            Object Collection = obj;

#if !NET20
            Object Key = Parameters == null ? null : Parameters.FirstOrDefault();
#else
            Object Key = Parameters == null ? null : Linq2.FirstOrDefault(Parameters);
#endif
            if (Collection == null)
            {
                return(null);
            }

            if (Collection is String)
            {
                Int32?index = UniConvert.ToInt32N(Key);
                if (index == null || index < 0)
                {
                    return(null);
                }

                String str = (String)Collection;
                if (index >= str.Length)
                {
                    return(null);
                }

                return(str[index.Value]);
            }

            if (Collection is DynLanObject)
            {
                DynLanObject DynLanObj = Collection as DynLanObject;

                if (DynLanObj.TotalCount == 0)
                {
                    return(null);
                }

                /*IDictionary<String, Object> dict = ((DynLanObject)Collection).Values;
                 * if (dict.Count == 0)
                 *  return null;*/

                String finalKey = ((String)(Key.GetType() == typeof(String) ? Key :
                                            Convert.ChangeType(Key, typeof(String), System.Globalization.CultureInfo.InvariantCulture)));

                return(DynLanObj[finalKey]);
            }

            if (Collection is IDictionaryWithGetter)
            {
                IDictionaryWithGetter dict = Collection as IDictionaryWithGetter;
                if (dict.CanGetValueFromDictionary(Key))
                {
                    return(dict.GetValueFromDictionary(Key));
                }
            }
            else if (Collection is IDictionary)
            {
                IDictionary dict = (IDictionary)Collection;
                if (dict.Count == 0)
                {
                    return(null);
                }

                Type[] arguments = dict.GetType().GetGenericArguments();
                Type   keyType   = arguments[0];

                Object finalKey = Key.GetType() == keyType ? Key :
                                  Convert.ChangeType(Key, keyType, System.Globalization.CultureInfo.InvariantCulture);

                return(dict[finalKey]);
            }
            else if (Collection is IDictionary <string, object> )
            {
                IDictionary <string, object> dict = (IDictionary <string, object>)Collection;
                if (dict.Count == 0)
                {
                    return(null);
                }
                return(dict[UniConvert.ToString(Key)]);
            }

            if (Collection is IList)
            {
                Int32?index = UniConvert.ToInt32N(Key);
                if (index == null || index < 0)
                {
                    return(null);
                }

                IList list = (IList)Collection;
                if (index >= list.Count)
                {
                    return(null);
                }

                return(list[index.Value]);
            }

            if (Collection is IEnumerable)
            {
                Int32?index = UniConvert.ToInt32N(Key);
                if (index == null || index < 0)
                {
                    return(null);
                }

                Int32 i = -1;
                foreach (Object item in ((IEnumerable)Collection))
                {
                    i++;
                    if (i == index.Value)
                    {
                        return(item);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        ////////////////////////////////////////////////////////////////////////

        public static Object Execute(PainContext EvaluateContext, Object obj, IList <Object> Parameters)
        {
            Object Collection = obj;
            Object value      = Parameters != null && Parameters.Count > 0 ? Parameters[0] : null;
            Object Key        = Parameters != null && Parameters.Count > 1 ? Parameters[1] : null;

            if (Collection == null)
            {
                return(null);
            }

            if (Collection is PainObject)
            {
                PainObject painObj = Collection as PainObject;

                String finalKey = (String)(Key.GetType() == typeof(String) ? Key :
                                           Convert.ChangeType(Key, typeof(String), System.Globalization.CultureInfo.InvariantCulture));

                Object finValue = value == null ? null : (value.GetType() == typeof(Object) ? value :
                                                          Convert.ChangeType(value, typeof(Object), System.Globalization.CultureInfo.InvariantCulture));

                painObj[finalKey] = finValue;

                return(value);
            }

            if (Collection is IDictionary)
            {
                IDictionary dict = (IDictionary)Collection;

                Type[] arguments = dict.GetType().GetGenericArguments();
                Type   keyType   = arguments[0];
                Type   valueType = arguments[1];

                Object finalKey = Key.GetType() == keyType ? Key :
                                  Convert.ChangeType(Key, keyType, System.Globalization.CultureInfo.InvariantCulture);

                Object finValue = value == null ? null : (value.GetType() == valueType ? value :
                                                          Convert.ChangeType(value, valueType, System.Globalization.CultureInfo.InvariantCulture));

                lock (dict)
                {
                    dict.Remove(finalKey);
                    dict.Add(finalKey, finValue);
                }

                return(value);
            }

            if (Collection is IList)
            {
                Int32?index = UniConvert.ToInt32N(Key);
                if (index == null || index < 0)
                {
                    return(null);
                }

                IList list = (IList)Collection;
                if (index >= list.Count)
                {
                    return(null);
                }

                Type listType = MyTypeHelper.GetListType(list);

                Object finValue = value == null ? null : (value.GetType() == listType ? value :
                                                          Convert.ChangeType(value, listType, System.Globalization.CultureInfo.InvariantCulture));

                list[index.Value] = finValue;

                return(value);
            }

            return(null);
        }