Ejemplo n.º 1
0
        /// <summary>扩展接口转名值字典</summary>
        /// <param name="extend"></param>
        /// <param name="throwOnError">出错时是否抛出异常</param>
        /// <returns></returns>
        public static IDictionary <String, Object> ToDictionary(this IExtend extend, Boolean throwOnError = true)
        {
            // 泛型字典
            if (extend is IDictionary <String, Object> dictionary)
            {
                return(dictionary);
            }
            if (extend is ExtendDictionary edic)
            {
                return(edic.Items);
            }
            if (extend is IExtend3 ext3)
            {
                return(ext3.Items);
            }

            // IExtend2
            if (extend is IExtend2 ext2)
            {
                var dic = new Dictionary <String, Object>();
                foreach (var item in ext2.Keys)
                {
                    dic[item] = extend[item];
                }
                return(dic);
            }

            // 普通字典
            if (extend is IDictionary dictionary2)
            {
                var dic = new Dictionary <String, Object>();
                foreach (DictionaryEntry item in dictionary2)
                {
                    dic[item.Key + ""] = item.Value;
                }
                return(dic);
            }

            // 反射 Items
            var pi = extend.GetType().GetProperty("Items", BindingFlags.Instance | BindingFlags.Public /*| BindingFlags.NonPublic*/);

            if (pi != null && pi.PropertyType.As <IDictionary <String, Object> >())
            {
                return(pi.GetValue(extend, null) as IDictionary <String, Object>);
            }

            //var fi = extend.GetType().GetField("Items", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            //if (fi != null && fi.FieldType.As<IDictionary<String, Object>>()) return fi.GetValue(extend) as IDictionary<String, Object>;

            if (throwOnError)
            {
                throw new NotSupportedException($"不支持从类型[{extend.GetType().FullName}]中获取字典!");
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>扩展接口转名值字典</summary>
        /// <remarks>
        /// 需要注意,还有一个 Object.ToDictionary() 扩展,位于 CollectionHelper
        /// </remarks>
        /// <param name="extend">扩展对象</param>
        /// <returns></returns>
        public static IDictionary <String, Object> ToDictionary(this IExtend extend)
        {
            if (extend == null)
            {
                return(null);
            }

            // 泛型字典
            if (extend is IDictionary <String, Object> dictionary)
            {
                return(dictionary);
            }
            if (extend is ExtendDictionary edic)
            {
                return(edic.Items);
            }
            if (extend is IExtend3 ext3)
            {
                return(ext3.Items);
            }

            // IExtend2
            if (extend is IExtend2 ext2)
            {
                var dic = new Dictionary <String, Object>();
                foreach (var item in ext2.Keys)
                {
                    dic[item] = extend[item];
                }
                return(dic);
            }

            // 普通字典
            if (extend is IDictionary dictionary2)
            {
                var dic = new Dictionary <String, Object>();
                foreach (DictionaryEntry item in dictionary2)
                {
                    dic[item.Key + ""] = item.Value;
                }
                return(dic);
            }

            // 反射 Items
            var pis = extend.GetType().GetProperties(true);
            var pi  = pis.FirstOrDefault(e => e.Name == "Items");

            if (pi != null && pi.PropertyType.As <IDictionary <String, Object> >())
            {
                return(pi.GetValue(extend, null) as IDictionary <String, Object>);
            }

            // 反射属性
            return(new ExtendDictionary2 {
                Data = extend, Keys = pis.Select(e => e.Name).ToList()
            });

            //var dic2 = new Dictionary<String, Object>();
            //foreach (var item in pis)
            //{
            //    dic2[item.Name] = extend[item.Name];
            //}

            //return dic2;
        }