Ejemplo n.º 1
0
        /// <summary>
        /// eg:*必输字段,【shippingorderid】,字符串类型,出库单ID
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cInfo"></param>
        /// <param name="fInfo"></param>
        /// <param name="dict"></param>
        /// <returns></returns>
        private string GetFieldDesc(ApiClassInfo cInfo, List <FieldDescInfo> fInfo, Dictionary <string, string> dict)
        {
            if (cInfo == null)
            {
                return(null);
            }

            var           name = cInfo.Name;
            StringBuilder sb   = new StringBuilder();

            if (fInfo != null)
            {
                if (fInfo.FirstOrDefault(x => x.FieldName == name)?.IsRequired == 1)
                {
                    sb.AppendFormat("*{0},", R("必输字段", dict));
                }
            }
            sb.AppendFormat("【{0}】,", R(name, dict));
            sb.AppendFormat("{0},", GetTypeDesc(cInfo.TypeName, dict));
            if (fInfo != null)
            {
                if (cInfo.TypeName == typeof(String).FullName)
                {
                    if (fInfo.FirstOrDefault(x => x.FieldName == name)?.MaxLength != null)
                    {
                        sb.AppendFormat("{0}{1},", R("最大长度", dict), fInfo.FirstOrDefault(x => x.FieldName == name)?.MaxLength);
                    }
                }
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        private ApiClassInfo GenerateDictionary(Type dictionaryType, int size, Dictionary <Type, object> createdObjectReferences)
        {
            var  classInfo = new ApiClassInfo(dictionaryType, ApiPropertyType.ArrayObject);
            Type typeK     = typeof(object);
            Type typeV     = typeof(object);

            if (dictionaryType.IsGenericType)
            {
                Type[] genericArgs = dictionaryType.GetGenericArguments();
                typeK = genericArgs[0];
                typeV = genericArgs[1];
            }

            ObjectGeneratorEx objectGenerator = new ObjectGeneratorEx();

            for (int i = 0; i < size; i++)
            {
                var ci       = new ApiClassInfo(dictionaryType, ApiPropertyType.ClassObject);
                var newKey   = objectGenerator.GenerateObject(typeK, createdObjectReferences);
                var newValue = objectGenerator.GenerateObject(typeV, createdObjectReferences);
                newKey.Name   = "Key";
                newValue.Name = "Value";
                ci.Properties.Add(newKey);
                ci.Properties.Add(newValue);
                classInfo.Properties.Add(ci);
            }

            return(classInfo);
        }
Ejemplo n.º 3
0
        private ApiClassInfo GenerateArray(Type arrayType, int size, Dictionary <Type, object> createdObjectReferences)
        {
            var classInfo = new ApiClassInfo(arrayType, ApiPropertyType.ArrayObject);

            Type type = arrayType.GetElementType();
            ObjectGeneratorEx objectGenerator = new ObjectGeneratorEx();

            for (int i = 0; i < size; i++)
            {
                var ci = objectGenerator.GenerateObject(type, createdObjectReferences);
                classInfo.Properties.Add(ci);
            }

            return(classInfo);
        }
Ejemplo n.º 4
0
        private ApiClassInfo GenerateEnum(Type enumType)
        {
            //var possibleNames = Enum.GetNames(enumType);
            var possibleValues = Enum.GetValues(enumType);
            var classInfo      = new ApiClassInfo(enumType, ApiPropertyType.SimpleObject);

            for (int i = 0; i < possibleValues.Length; i++)
            {
                classInfo.Remark += ((int)possibleValues.GetValue(i)).ToString();
                if (i < (possibleValues.Length - 1))
                {
                    classInfo.Remark += ",";
                }
            }
            return(classInfo);
        }
Ejemplo n.º 5
0
        private ApiClassInfo GenerateTuple(Type type, Dictionary <Type, object> createdObjectReferences)
        {
            var classInfo = new ApiClassInfo(type, ApiPropertyType.ClassObject);

            Type[]            genericArgs     = type.GetGenericArguments();
            ObjectGeneratorEx objectGenerator = new ObjectGeneratorEx();

            for (int i = 0; i < genericArgs.Length; i++)
            {
                var ci = objectGenerator.GenerateObject(genericArgs[i], createdObjectReferences);
                ci.Name = $"Item{i + 1}";
                classInfo.Properties.Add(ci);
            }

            return(classInfo);
        }
Ejemplo n.º 6
0
        private ApiClassInfo GenerateCollection(Type collectionType, int size, Dictionary <Type, object> createdObjectReferences)
        {
            var  classInfo = new ApiClassInfo(collectionType, ApiPropertyType.ArrayObject);
            Type type      = collectionType.IsGenericType ?
                             collectionType.GetGenericArguments()[0] :
                             typeof(object);
            bool areAllElementsNull           = true;
            ObjectGeneratorEx objectGenerator = new ObjectGeneratorEx();

            for (int i = 0; i < size; i++)
            {
                var ci = objectGenerator.GenerateObject(type, createdObjectReferences);
                classInfo.Properties.Add(ci);
            }

            return(classInfo);
        }
Ejemplo n.º 7
0
        private ApiClassInfo GenerateKeyValuePair(Type keyValuePairType, Dictionary <Type, object> createdObjectReferences)
        {
            var classInfo = new ApiClassInfo(keyValuePairType, ApiPropertyType.ClassObject);

            Type[]            genericArgs     = keyValuePairType.GetGenericArguments();
            Type              typeK           = genericArgs[0];
            Type              typeV           = genericArgs[1];
            ObjectGeneratorEx objectGenerator = new ObjectGeneratorEx();
            var keyObject   = objectGenerator.GenerateObject(typeK, createdObjectReferences);
            var valueObject = objectGenerator.GenerateObject(typeV, createdObjectReferences);

            keyObject.Name   = "Key";
            valueObject.Name = "Value";
            classInfo.Properties.Add(keyObject);
            classInfo.Properties.Add(valueObject);

            return(classInfo);
        }
Ejemplo n.º 8
0
        private ApiClassInfo GenerateComplexObject(Type type, Dictionary <Type, object> createdObjectReferences)
        {
            var classInfo = new ApiClassInfo(type, ApiPropertyType.ClassObject);
            var at        = type.GetCustomAttributes(typeof(UdfModelAttribute), true);

            if (at.Length > 0)
            {
                classInfo.UdfModel = at[0] as UdfModelAttribute;
            }

            if (createdObjectReferences.TryGetValue(type, out var result))
            {
                // The object has been created already, just return it. This will handle the circular reference case.
                return(result as ApiClassInfo);
            }

            createdObjectReferences.Add(type, classInfo);

            var list = SetPublicProperties(type, createdObjectReferences);

            if (list.Count > 0)
            {
                classInfo.Properties.AddRange(list);
            }

            list = SetPublicFields(type, createdObjectReferences);
            if (list.Count > 0)
            {
                classInfo.Properties.AddRange(list);
            }

            if (classInfo.Properties.Count <= 0)
            {
                classInfo.JsonType = ApiPropertyType.SimpleObject;
            }


            return(classInfo);
        }
Ejemplo n.º 9
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);
        }