Exemplo n.º 1
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            String name = binder.Name;

            if (!m_dictionary.TryGetValue(name, out result))
            {
                //以is开头的名称都判断是布尔类型
                if (name.ToLower().StartsWith("is"))
                {
                    result = false;
                }
                else
                {
                    result = String.Empty;
                }
                return(true);
            }

            if (result is String)
            {
                //处理是否布尔类型
                String stringResult = result as String;
                if (!String.IsNullOrWhiteSpace(stringResult))
                {
                    if (stringResult.ToLower() == "true" || stringResult.ToLower() == "false")
                    {
                        result = Convert.ToBoolean(stringResult.ToLower());
                        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);
        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            String name = binder.Name;
            if (!m_dictionary.TryGetValue(name, out result))
            {
                //以is开头的名称都判断是布尔类型
                if (name.ToLower().StartsWith("is"))
                {
                    result = false;
                }
                else
                {
                    result = String.Empty;
                }
                return true;
            }

            if (result is String)
            {
                //处理是否布尔类型
                String stringResult = result as String;
                if (!String.IsNullOrWhiteSpace(stringResult))
                {
                    if (stringResult.ToLower() == "true" || stringResult.ToLower() == "false")
                    {
                        result = Convert.ToBoolean(stringResult.ToLower());
                        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;
        }