Exemplo n.º 1
0
        public object CreateObject(Func <ApiClassInfo, Task <List <FieldDescInfo> > > func, Dictionary <string, string> dict, ApiClassInfo info = null)
        {
            if (info == null)
            {
                info = this;
            }

            var o = new ObjectGeneratorEx();

            if (info.JsonType == ApiPropertyType.SimpleObject)
            {
                var t = Type.GetType(info.TypeName);
                //如果没有类型,则应该是无属性的类,则序列化为对象
                if (t == null)
                {
                    var obj = o.GenerateSimpleObject(typeof(object));
                    return(obj);
                }
                else
                {
                    var obj = o.GenerateSimpleObject(t);
                    return(obj);
                }
            }
            else if (info.JsonType == ApiPropertyType.ArrayObject)
            {
                var len  = info.Properties.Count;
                var objs = new object[len];
                for (int i = 0; i < len; i++)
                {
                    if (info.Properties[i] != null)
                    {
                        objs[i] = info.Properties[i].CreateObject(func, dict);
                    }
                }
                return(objs);
            }
            else if (info.JsonType == ApiPropertyType.ClassObject)
            {
                if (info.Properties.Count <= 0)
                {
                    return(new object());
                }

                dynamic dobj = new System.Dynamic.ExpandoObject();
                var     d    = (IDictionary <string, object>)dobj;
                var     f    = func(info).Result;

                foreach (var p in info.Properties)
                {
                    if (p != null)
                    {
                        //对属性就行整理
                        if (p.JsonType == ApiPropertyType.SimpleObject)
                        {
                            var s = GetFieldDesc(p, f, dict);
                            if (!string.IsNullOrEmpty(s))
                            {
                                d[p.Name] = s;
                            }
                            else
                            {
                                d[p.Name] = p.CreateObject(func, dict);
                            }
                        }
                        else
                        {
                            d[p.Name] = p.CreateObject(func, dict);
                        }
                    }
                }

                return(d);
            }

            return(null);
        }