Exemple #1
0
        private void GetInfo(PropertyConfig config, Type type, MemberInfo field)
        {
            string name   = $"{type.FullName}.{field.Name}";
            var    member = XmlMember.Find(name);

            if (member != null)
            {
                config.Description = member.Summary;
                config.Caption     = member.DisplayName;
                config.Remark      = member.Remark;
            }
            var ca = field.GetAttribute <DisplayNameAttribute>();

            if (ca != null)
            {
                config.Caption = ca.DisplayName;
            }
            var ct = field.GetAttribute <CategoryAttribute>();

            if (ct != null)
            {
                config.Group = ct.Category;
            }
            var de = field.GetAttribute <DescriptionAttribute>();

            if (de != null)
            {
                config.Description = de.Description;
            }
        }
Exemple #2
0
        private void GetInfo(ClassifyConfig config, Type type)
        {
            var member = XmlMember.Find(type);

            if (member != null)
            {
                config.Caption     = member.DisplayName;
                config.Description = member.Summary;
                config.Remark      = member.Remark;
            }
            var ca = type.GetAttribute <DisplayNameAttribute>();

            if (ca != null)
            {
                config.Caption = ca.DisplayName;
            }
            var ct = type.GetAttribute <CategoryAttribute>();

            if (ct != null)
            {
                config.Classify = ct.Category;
            }
            var de = type.GetAttribute <DescriptionAttribute>();

            if (de != null)
            {
                config.Description = de.Description;
            }
        }
Exemple #3
0
        static void ReadEntity(ref 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))
            {
                return;
            }

            if (type.IsEnum)
            {
                typeDocument = new TypeDocument
                {
                    IsEnum     = true,
                    IsBaseType = true,
                    Name       = type.Name,
                    ClassName  = type.GetFullTypeName()
                };
            }
            else if (type == typeof(string))
            {
                typeDocument = new TypeDocument
                {
                    IsBaseType = true,
                    Name       = type.Name,
                    ClassName  = type.GetFullTypeName()
                };
                return;
            }
            else if (type.IsBaseType())
            {
                typeDocument = new TypeDocument
                {
                    IsBaseType = true,
                    Name       = type.Name,
                    ClassName  = type.GetFullTypeName()
                };
                return;
            }
            if (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 (_lock.TryGetValue(type, out var doc2))
            {
                typeDocument = doc2;
                return;
            }
            _lock.Add(type, typeDocument);
            if (type.IsArray)
            {
                ReadEntity(ref typeDocument, type.Assembly.GetType(type.FullName.Split('[')[0]));
                typeDocument.IsArray = true;
                return;
            }
            if (type.IsGenericType && !type.IsValueType &&
                type.GetGenericTypeDefinition().GetInterface(typeof(IEnumerable <>).FullName) != null)
            {
                ReadEntity(ref typeDocument, type.GetGenericArguments().Last());
                typeDocument.IsArray = true;
                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 = GetCustomAttribute <DataContractAttribute>(type);
            var jo = GetCustomAttribute <JsonObjectAttribute>(type);

            foreach (var property in type.GetProperties(AllFlags))
            {
                if (property.IsSpecialName)
                {
                    continue;
                }
                CheckMember(typeDocument, type, property, property.PropertyType, jo != null, dc != null);
            }
            foreach (var field in type.GetFields(AllFlags))
            {
                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));
        }
Exemple #4
0
        static 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 jp1 = GetCustomAttribute <JsonPropertyAttribute>(member);
            var jp2 = GetCustomAttribute <JsonPropertyNameAttribute>(member);
            var dm  = GetCustomAttribute <DataMemberAttribute>(member);

            if (json)
            {
                var ji1 = GetCustomAttribute <Newtonsoft.Json.JsonIgnoreAttribute>(member);
                var ji2 = GetCustomAttribute <System.Text.Json.Serialization.JsonIgnoreAttribute>(member);
                if (ji1 != null || ji2 != null)
                {
                    return(null);
                }
            }
            else if (dc)
            {
                var id = GetCustomAttribute <IgnoreDataMemberAttribute>(member);
                if (id != null)
                {
                    return(null);
                }
            }

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

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

            try
            {
                var type = memType;
                if (memType.IsArray)
                {
                    isArray = true;
                    type    = type.Assembly.GetType(type.FullName.Split('[')[0]);
                }
                else if (type.IsGenericType)
                {
                    if (memType.GetGenericTypeDefinition().IsSupperInterface(typeof(IEnumerable <>)))
                    {
                        isArray = true;
                        type    = type.GetGenericArguments()[0];
                    }
                    else if (memType.GetGenericTypeDefinition().IsSupperInterface(typeof(ICollection <>)))
                    {
                        isArray = true;
                        type    = type.GetGenericArguments()[0];
                    }
                    else if (memType.GetGenericTypeDefinition().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.IsBaseType = true;
                    field.IsEnum     = true;
                }
                else if (type.IsBaseType())
                {
                    field.IsBaseType = true;
                }
                else if (!isDictionary)
                {
                    if (checkBase)
                    {
                        field = ReadEntity(type, member.Name);
                    }
                    field.IsBaseType = false;
                }
                field.TypeName = ReflectionHelper.GetTypeName(type);
            }
            catch
            {
                field.TypeName = "object";
            }
            if (isArray)
            {
                field.TypeName += "[]";
                field.IsArray   = true;
            }
            else if (isDictionary)
            {
                field.TypeName   = "Dictionary";
                field.IsArray    = false;
                field.IsBaseType = false;
            }

            field.Name      = member.Name;
            field.JsonName  = jp1?.PropertyName ?? jp2?.Name ?? dm?.Name ?? member.Name;
            field.ClassName = ReflectionHelper.GetTypeName(memType);


            var rule = GetCustomAttribute <DataRuleAttribute>(member);

            if (rule != null)
            {
                field.CanNull = rule.CanNull;
                field.Regex   = rule.Regex;
                field.Min     = rule.Min;
                field.Max     = rule.Max;
            }
            document.Fields.Add(member.Name, field);

            return(field);
        }