方法'ToDictionary'没有重载需要0个参数

时间:2016-04-21 08:29:11

标签: c# generics lambda

实际上我应该工作,从我的角度来看,我不知道什么是错的,问题是itemvalue没有ToDictionary()

也许你可以看一下?!

方法签名: public static IDictionary<string, object> ListO(this object instance)

我的代码:

 if (instance == null)
                throw new NullReferenceException();

            var result = instance as IDictionary<string, object>;
            if (result != null)
                return result;

            return instance.GetType()
                .GetProperties()
                .ToDictionary(x => x.Name, x =>
                {
                    object value = x.GetValue(instance);

                    if (value != null)
                    {
                        var valueType = value.GetType();

                        // Whe should manually check for string type because IsPrimitive will return false in case of string
                        if (valueType.IsPrimitive || valueType == typeof(string))
                        {

                            return value;
                        }
                        else if (valueType.GetInterfaces().Any(t => t == typeof(IEnumerable)))
                        {
                            List<object> elements = new List<object>();

                            foreach (var item in value as IEnumerable)
                            {
                                elements.Add(item.ToDictionary());//problem here
                            }

                            return elements;
                        }
                        else
                        {
                            return value.ToDictionary();//problem here
                        }

                    }
                    return null;
                }); 

我明白了:

  

方法'ToDictionary'没有重载需要0个参数

我错了什么?

0 个答案:

没有答案