コード例 #1
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);
        }
コード例 #2
0
        private ApiClassInfo GenerateNullable(Type nullableType, Dictionary <Type, object> createdObjectReferences)
        {
            Type type = nullableType.GetGenericArguments()[0];
            ObjectGeneratorEx objectGenerator = new ObjectGeneratorEx();
            var ci = objectGenerator.GenerateObject(type, createdObjectReferences);

            ci.CanNull = true;
            return(ci);
        }
コード例 #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);
        }
コード例 #4
0
        private List <ApiClassInfo> SetPublicFields(Type type, Dictionary <Type, object> createdObjectReferences)
        {
            List <ApiClassInfo> infos = new List <ApiClassInfo>();

            FieldInfo[]       fields          = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
            ObjectGeneratorEx objectGenerator = new ObjectGeneratorEx();

            foreach (FieldInfo field in fields)
            {
                var ci = objectGenerator.GenerateObject(field.FieldType, createdObjectReferences);
                ci.Name = field.Name;
                infos.Add(ci);
            }

            return(infos);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #7
0
        private List <ApiClassInfo> SetPublicProperties(Type type, Dictionary <Type, object> createdObjectReferences)
        {
            List <ApiClassInfo> infos = new List <ApiClassInfo>();

            PropertyInfo[]    properties      = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            ObjectGeneratorEx objectGenerator = new ObjectGeneratorEx();

            foreach (PropertyInfo property in properties)
            {
                if (property.CanWrite || property.CanRead)
                {
                    var ci = objectGenerator.GenerateObject(property.PropertyType, createdObjectReferences);
                    ci.Name = property.Name;
                    infos.Add(ci);
                }
            }
            return(infos);
        }
コード例 #8
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);
        }
コード例 #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);
        }
コード例 #10
0
ファイル: ApiHelper.cs プロジェクト: 775083111/GMS
        /// <summary>
        /// 获取类信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ApiClassInfo GetApiInfoByType(Type type)
        {
            ObjectGeneratorEx ex = new ObjectGeneratorEx();

            return(ex.GenerateObject(type));
        }