示例#1
0
        private void ReadEntity(TypeDocument typeDocument, Type type)
        {
            if (type == typeof(object))
            {
                return;
            }
            if (type.BaseType != typeof(object) && !type.BaseType.IsInterface)
            {
                ReadEntity(typeDocument, type.BaseType);
            }
            var dc = type.GetAttribute <DataContractAttribute>();
            var jo = type.GetAttribute <JsonObjectAttribute>();

            foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                if (property.IsSpecialName)
                {
                    continue;
                }
                CheckMember(typeDocument, type, property, property.PropertyType, jo != null, dc != null);
            }
            foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                if (field.IsSpecialName)
                {
                    continue;
                }
                CheckMember(typeDocument, type, field, field.FieldType, jo != null, dc != null);
            }
        }
示例#2
0
        private TypeDocument ReadEntity(Type type, string name)
        {
            var typeDocument = new TypeDocument
            {
                Name       = name,
                TypeName   = ReflectionHelper.GetTypeName(type),
                ClassName  = ReflectionHelper.GetTypeName(type),
                ObjectType = ObjectType.Object
            };

            //if (typeDocs.TryGetValue(type, out var doc))
            //    return doc;
            ReadEntity(typeDocument, type);
            return(typeDocument);
        }
示例#3
0
        private TypeDocument ReadEntity(Type type, string name)
        {
            if (type == typeof(void))
            {
                return(null);
            }
            if (!IsLetter(type.Name[0]))
            {
                return(null);
            }
            var typeDocument = new TypeDocument
            {
                Name       = name,
                TypeName   = ReflectionHelper.GetTypeName(type),
                ClassName  = ReflectionHelper.GetTypeName(type),
                ObjectType = ObjectType.Object
            };

            typeDocument.Copy(XmlMember.Find(type));
            ReadEntity(typeDocument, type);
            return(typeDocument);
        }
示例#4
0
        TypeDocument CheckMember(TypeDocument document, Type parent, MemberInfo member, Type memType, bool json, bool dc, bool checkBase = true)
        {
            if (document.Fields.ContainsKey(member.Name))
            {
                return(null);
            }
            var jp = member.GetAttribute <JsonPropertyAttribute>();
            var dm = member.GetAttribute <DataMemberAttribute>();

            if (json)
            {
                var ji = member.GetAttribute <JsonIgnoreAttribute>();
                if (ji != null)
                {
                    return(null);
                }
                if (jp == null)
                {
                    return(null);
                }
            }
            else if (dc)
            {
                var id = member.GetAttribute <IgnoreDataMemberAttribute>();
                if (id != null)
                {
                    return(null);
                }
            }

            var field = new TypeDocument();
            var doc   = XmlMember.Find(parent, member.Name);

            field.Copy(doc);
            bool isArray      = false;
            bool isDictionary = false;

            try
            {
                Type type = memType;
                if (memType.IsArray)
                {
                    isArray = true;
                    type    = type.Assembly.GetType(type.FullName.Split('[')[0]);
                }
                else if (type.IsGenericType)
                {
                    if (memType.IsSupperInterface(typeof(ICollection <>)))
                    {
                        isArray = true;
                        type    = type.GetGenericArguments()[0];
                    }
                    else if (memType.IsSupperInterface(typeof(IDictionary <,>)))
                    {
                        var fields = type.GetGenericArguments();
                        field.Fields.Add("Key", ReadEntity(fields[0], "Key"));
                        field.Fields.Add("Value", ReadEntity(fields[1], "Value"));
                        isDictionary = true;
                        checkBase    = false;
                    }
                }
                if (type.IsEnum)
                {
                    if (checkBase)
                    {
                        field = ReadEntity(type, member.Name);
                    }
                    field.ObjectType = ObjectType.Base;
                    field.IsEnum     = true;
                }
                else if (type.IsBaseType())
                {
                    field.ObjectType = ObjectType.Base;
                }
                else if (!isDictionary)
                {
                    if (checkBase)
                    {
                        field = ReadEntity(type, member.Name);
                    }
                    field.ObjectType = ObjectType.Object;
                }
                field.TypeName = ReflectionHelper.GetTypeName(type);
            }
            catch
            {
                field.TypeName = "object";
            }
            if (isArray)
            {
                field.TypeName  += "[]";
                field.ObjectType = ObjectType.Array;
            }
            else if (isDictionary)
            {
                field.TypeName   = "Dictionary";
                field.ObjectType = ObjectType.Dictionary;
            }

            field.Name      = member.Name;
            field.JsonName  = member.Name;
            field.ClassName = ReflectionHelper.GetTypeName(memType);

            if (!string.IsNullOrWhiteSpace(dm?.Name))
            {
                field.JsonName = dm.Name;
            }
            if (!string.IsNullOrWhiteSpace(jp?.PropertyName))
            {
                field.JsonName = jp.PropertyName;
            }
            var rule = member.GetAttribute <DataRuleAttribute>();

            if (rule != null)
            {
                field.CanNull = rule.CanNull;
                field.Regex   = rule.Regex;
                if (rule.Min != long.MinValue)
                {
                    field.Min = rule.Min;
                }
                if (rule.Max != long.MinValue)
                {
                    field.Max = rule.Max;
                }
                if (rule.MinDate != DateTime.MinValue)
                {
                    field.MinDate = rule.MinDate;
                }
                if (rule.MaxDate != DateTime.MaxValue)
                {
                    field.MaxDate = rule.MaxDate;
                }
            }
            document.Fields.Add(member.Name, field);

            return(field);
        }
示例#5
0
        private void ReadEntity(TypeDocument typeDocument, Type type)
        {
            if (type == null || type.IsAutoClass || !IsLetter(type.Name[0]) ||
                type.IsInterface || type.IsMarshalByRef || type.IsCOMObject ||
                type == typeof(object) || type == typeof(void) ||
                type == typeof(ValueType) || type == typeof(Type) || type == typeof(Enum) ||
                type.Namespace == "System" || type.Namespace?.Contains("System.") == true)
            {
                return;
            }
            if (typeDocs.TryGetValue(type, out var doc))
            {
                foreach (var field in doc.Fields)
                {
                    if (typeDocument.Fields.ContainsKey(field.Key))
                    {
                        typeDocument.Fields[field.Key] = field.Value;
                    }
                    else
                    {
                        typeDocument.Fields.Add(field.Key, field.Value);
                    }
                }
                return;
            }
            if (typeDocs2.TryGetValue(type, out var _))
            {
                ZeroTrace.WriteError("ReadEntity", "over flow", type.Name);

                return;
            }

            typeDocs2.Add(type, typeDocument);
            if (type.IsArray)
            {
                ReadEntity(typeDocument, type.Assembly.GetType(type.FullName.Split('[')[0]));
                return;
            }
            if (type.IsGenericType && !type.IsValueType &&
                type.GetGenericTypeDefinition().GetInterface(typeof(IEnumerable <>).FullName) != null)
            {
                ReadEntity(typeDocument, type.GetGenericArguments().Last());
                return;
            }

            XmlMember.Find(type);
            if (type.IsEnum)
            {
                foreach (var field in type.GetFields(BindingFlags.Static | BindingFlags.Public))
                {
                    if (field.IsSpecialName)
                    {
                        continue;
                    }
                    var info = CheckMember(typeDocument, type, field, field.FieldType, false, false, false);
                    if (info != null)
                    {
                        info.TypeName = "int";
                        info.Example  = ((int)field.GetValue(null)).ToString();
                        info.JsonName = null;
                    }
                }
                typeDocs.Add(type, new TypeDocument
                {
                    fields = typeDocument.fields?.ToDictionary(p => p.Key, p => p.Value)
                });
                typeDocument.Copy(XmlMember.Find(type));
                return;
            }

            var dc = type.GetAttribute <DataContractAttribute>();
            var jo = type.GetAttribute <JsonObjectAttribute>();

            foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (property.IsSpecialName)
                {
                    continue;
                }
                CheckMember(typeDocument, type, property, property.PropertyType, jo != null, dc != null);
            }
            foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (!char.IsLetter(field.Name[0]) || field.IsSpecialName)
                {
                    continue;
                }
                CheckMember(typeDocument, type, field, field.FieldType, jo != null, dc != null);
            }

            typeDocs.Add(type, new TypeDocument
            {
                fields = typeDocument.fields?.ToDictionary(p => p.Key, p => p.Value)
            });
            typeDocument.Copy(XmlMember.Find(type));
        }
示例#6
0
        void CheckMember(TypeDocument document, Type parent, MemberInfo member, Type memType, bool json, bool dc)
        {
            if (!IsLetter(member.Name[0]))
            {
                return;
            }
            var field = new TypeDocument();

            try
            {
                Type type = memType;
                if (memType.IsSubclassOf(typeof(IList <>)))
                {
                    type = type.GetGenericArguments()[0];
                }
                if (memType.IsSubclassOf(typeof(IDictionary <,>)))
                {
                    type = type.GetGenericArguments()[1];
                }
                if (memType.IsArray)
                {
                    type = type.MakeArrayType();
                }
                if (type.IsEnum)
                {
                    field            = ReadEntity(type, member.Name);
                    field.ObjectType = ObjectType.Base;
                    field.IsEnum     = type.IsEnum;
                }
                else if (type.IsBaseType())
                {
                    field.ObjectType = ObjectType.Base;
                    field.TypeName   = ReflectionHelper.GetTypeName(type);
                }
                else
                {
                    field            = ReadEntity(type, member.Name);
                    field.ObjectType = ObjectType.Object;
                }
            }
            catch
            {
                field.TypeName = "object";
            }
            field.Copy(XmlMember.Find(parent, member.Name, member is PropertyInfo ? "P" : "F"));
            field.Name      = field.JsonName = member.Name;
            field.ClassName = ReflectionHelper.GetTypeName(memType);
            if (json)
            {
                var ji = member.GetAttribute <JsonIgnoreAttribute>();
                if (ji != null)
                {
                    return;
                }
                var jp = member.GetAttribute <JsonPropertyAttribute>();
                if (jp == null)
                {
                    return;
                }
                if (!string.IsNullOrWhiteSpace(jp.PropertyName))
                {
                    field.JsonName = jp.PropertyName;
                }
            }
            else if (dc)
            {
                var id = member.GetAttribute <IgnoreDataMemberAttribute>();
                if (id != null)
                {
                    return;
                }
                var dm = member.GetAttribute <DataMemberAttribute>();
                if (dm != null && !string.IsNullOrWhiteSpace(dm.Name))
                {
                    field.JsonName = dm.Name;
                }
            }
            if (memType.IsSubclassOf(typeof(IList <>)))
            {
                field.ObjectType = ObjectType.Array;
            }
            else if (memType.IsSubclassOf(typeof(IDictionary <,>)))
            {
                field.ObjectType = ObjectType.Dictionary;
            }
            else if (memType.IsArray)
            {
                field.ObjectType = ObjectType.Array;
            }
            var rule = member.GetAttribute <DataRuleAttribute>();

            if (rule != null)
            {
                field.CanNull = rule.CanNull;
                field.Regex   = rule.Regex;
                if (rule.Min != long.MinValue)
                {
                    field.Min = rule.Min;
                }
                if (rule.Max != long.MinValue)
                {
                    field.Max = rule.Max;
                }
                if (rule.MinDate != DateTime.MinValue)
                {
                    field.MinDate = rule.MinDate;
                }
                if (rule.MaxDate != DateTime.MaxValue)
                {
                    field.MaxDate = rule.MaxDate;
                }
            }
            document.Fields.Add(member.Name, field);
        }