示例#1
0
        ////////////////////////////////////////////////////////////////////////

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

            String propertyPath = UniConvert.ToUniString(Parameters != null && Parameters.Count > 0 ? Parameters[0] : null);
            Object value        = (Parameters != null && Parameters.Count > 1 ? Parameters[1] : null);

            if (obj is IDictionaryWithGetter)
            {
                IDictionaryWithGetter dict = obj as IDictionaryWithGetter;
                if (dict.CanSetValueToDictionary(propertyPath))
                {
                    dict[propertyPath] = value;
                    isValueSet         = true;
                }
            }

            else if (obj is IDictionary)
            {
                IDictionary dict = obj as IDictionary;
                dict[propertyPath] = value;
                isValueSet         = true;
            }

            else if (obj is IDictionary <string, object> )
            {
                IDictionary <string, object> dict = obj as IDictionary <string, object>;
                dict[propertyPath] = value;
                isValueSet         = true;
            }

            if (obj is DynLanObject)
            {
#if CASE_INSENSITIVE
                propertyPath = propertyPath.ToUpper();
#endif
                DynLanObject DynLanObj = obj as DynLanObject;
                DynLanObj[propertyPath] = value;
                return(null);
            }

            if (!isValueSet)
            {
                isValueSet = RefSensitiveHelper.I.SetValue(obj, propertyPath, value);
            }

            //if (!isValueSet)
            //    isValueSet = RefUnsensitiveHelper.I.SetValue(obj, propertyPath, value);

            if (isValueSet)
            {
                return(value);
            }

            return(null);
        }
示例#2
0
        public static Object GetValueFromObject(
            Object Obj,
            String PropertyPath,
            Int32 ParametersCount,
            Boolean ExtenderExists,
            out Boolean FoundValue)
        {
            FoundValue = false;

            if (Obj == null)
            {
                FoundValue = true;
                return(null);
            }

            if (Obj is EmptyObject)
            {
                throw new NotImplementedException();
            }

            if (Obj is IDictionaryWithGetter)
            {
                IDictionaryWithGetter dict = Obj as IDictionaryWithGetter;
                Boolean contains           = dict.Contains(PropertyPath);

                if ((contains || !ExtenderExists) && dict.CanGetValueFromDictionary(PropertyPath))
                {
                    FoundValue = true;
                    return(dict.GetValueFromDictionary(PropertyPath));
                }
            }

            else if (Obj is IDictionary)
            {
                IDictionary dict = Obj as IDictionary;
                if (dict.Contains(PropertyPath))
                {
                    FoundValue = true;
                    return(dict[PropertyPath]);
                }
            }

            else if (Obj is IDictionary <string, object> )
            {
                IDictionary <string, object> dict = Obj as IDictionary <string, object>;
                if (dict.ContainsKey(PropertyPath))
                {
                    FoundValue = true;
                    return(dict[PropertyPath]);
                }
            }

            else if (Obj is DynLanObject)
            {
#if CASE_INSENSITIVE
                PropertyPath = PropertyPath.ToUpper();
#endif
                DynLanObject DynLanObj = Obj as DynLanObject;
                if (DynLanObj.Contains(PropertyPath))
                {
                    FoundValue = true;
                    return(DynLanObj[PropertyPath]);
                }
            }

            Object val = RefSensitiveHelper.I.GetValueOrMethod(
                Obj,
                PropertyPath,
                ParametersCount,
                out FoundValue);

            return(val);
        }
示例#3
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);
        }
        ////////////////////////////////////////////////////////////////////////

        public static Object Execute(DynContext 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 DynLanObject)
            {
                DynLanObject DynLanObj = Collection as DynLanObject;

                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));

                DynLanObj[finalKey] = finValue;

                return(value);
            }

            if (Collection is IDictionaryWithGetter)
            {
                IDictionaryWithGetter dict = (IDictionaryWithGetter)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));

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

                return(value);
            }

            else 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);
            }

            else if (Collection is IDictionary <string, object> )
            {
                IDictionary <string, object> dict = (IDictionary <string, object>)Collection;

                lock (dict)
                {
                    string finalKey = UniConvert.ToString(Key);
                    dict.Remove(finalKey);
                    dict.Add(finalKey, value);
                }

                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);
        }