示例#1
0
                public override bool TryGetMember(GetMemberBinder binder, out object result)
                {
                    if (!_dictionary.TryGetValue(binder.Name, out result))
                    {
                        return(false);
                    }

                    var dictionary = result as IDictionary <string, object>;

                    if (dictionary != null)
                    {
                        result = new DynamicJsonObject(dictionary);
                        return(true);
                    }

                    var list = result as IList;

                    if (list != null)
                    {
                        if (list.Count > 0 && list[0] is IDictionary <string, object> )
                        {
                            result = list.Cast <IDictionary <string, object> >().Select(x => new DynamicJsonObject(x)).ToArray();
                        }
                        else
                        {
                            result = list.Cast <object>().Select <object, object>(x => (dynamic)x).ToArray();
                        }
                    }

                    return(true);
                }
示例#2
0
        public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
        {
            result = this.Dictionary[binder.Name];

            if (result is IDictionary <string, object> )
            {
                result = new DynamicJsonObject(result as IDictionary <string, object>);
            }
            else if (result is ArrayList && (result as ArrayList) is IDictionary <string, object> )
            {
                result = new List <DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary <string, object>)));
            }
            else if (result is ArrayList)
            {
                result = new List <object>((result as ArrayList).ToArray());
            }

            return(this.Dictionary.ContainsKey(binder.Name));
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            //if (!_dictionary.TryGetValue(binder.Name, out result))
            //{
            //    // return null to avoid exception.  caller can check for null this way...
            //    result = null;
            //    return true;
            //}

            //var dictionary = result as IDictionary<string, object>;
            //if (dictionary != null)
            //{
            //    result = new DynamicJsonObject(dictionary);
            //    return true;
            //}

            //var arrayList = result as ArrayList;
            //if (arrayList != null && arrayList.Count > 0)
            //{
            //    if (arrayList[0] is IDictionary<string, object>)
            //        result = new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)));
            //    else
            //        result = new List<object>(arrayList.Cast<object>());
            //}

            //return true;
            result = this._dictionary.FirstOrDefault(dictionary => dictionary.Key.ToLower() == binder.Name.ToLower()).Value;

            if (result is IDictionary <string, object> )
            {
                result = new DynamicJsonObject(result as IDictionary <string, object>);
            }
            else if (result is ArrayList && (result as ArrayList) is IDictionary <string, object> )
            {
                result = new List <DynamicJsonObject>((result as ArrayList).Cast <IDictionary <string, object> >().Select(x => new DynamicJsonObject(x)));
            }
            else if (result is ArrayList)
            {
                result = new List <DynamicJsonObject>((result as ArrayList).Cast <DynamicJsonObject>());
            }

            return(true);
        }