Пример #1
0
        private IEnumerable <PropInfo> GetPropInfos(Type type)
        {
            var fieldNames = type.GetCustomAttribute <RelatedFieldAttribute>()?.FieldNames.ToList();

            if (fieldNames == null)
            {
                fieldNames = type.GetProperties().Select(v => v.Name).ToList();
                var baseFieldNames = type.BaseType.GetProperties().Select(v => v.Name).ToList();
                fieldNames = fieldNames.Where(v => !baseFieldNames.Any(r => r == v)).ToList();
            }

            if (!fieldNames.Contains("Id"))
            {
                fieldNames.Add("Id");
            }
            foreach (var item in fieldNames)
            {
                var property = type.GetProperty(item);

                yield return(new PropInfo
                {
                    Name = item,
                    TypeName = T4Help.GetTypeName(property.PropertyType),
                    Summary = T4Help.GetPropertySummary(property, XmlDocDir)
                });
            }
        }
Пример #2
0
        public IEnumerable <PropInfo> GetPropInfos(Type type)
        {
            var instance = type.Assembly.CreateInstance(type.FullName);

            foreach (var item in type.GetProperties())
            {
                if (item.Name == "Id")
                {
                    continue;
                }

                if (item.GetCustomAttribute <ExcludeAttribute>() != null)
                {
                    continue;
                }

                var summary = T4Help.GetPropertySummary(item, XmlDocDir);

                var defaultValue = instance.GetValue(item.Name)?.ToString();
                if (defaultValue == null)
                {
                    defaultValue = "null";
                }
                if (T4Help.GetNullableType(item.PropertyType) == typeof(string))
                {
                    defaultValue = $"\"{defaultValue}\"";
                }
                if (T4Help.GetNullableType(item.PropertyType) == typeof(string))
                {
                    defaultValue = $"\"{defaultValue}\"";
                }
                yield return(new PropInfo()
                {
                    Summary = summary,
                    AttrInfos = GetAttrInfos(item),
                    TypeName = T4Help.GetTypeName(item.PropertyType),
                    Name = item.Name,
                    DefaultValue = defaultValue
                });

                if (TryGetAttribute <RelatedToAttribute>(item, out var relatedToAttribute))
                {
                    var relateTypeName = relatedToAttribute.RelatedType.Name;
                    yield return(new PropInfo()
                    {
                        Summary = summary,
                        TypeName = $"{relateTypeName}ViewModel",
                        Name = item.Name.Replace("_Id", "")
                    });
                }
            }

            if (typeof(ITreeEntity).IsAssignableFrom(type))
            {
                yield return new PropInfo
                       {
                           Name         = "HasTreeChildren",
                           DefaultValue = "false",
                           Summary      = "是否有下级",
                           TypeName     = "bool"
                       }
            }
            ;

            if (typeof(IHaveMultiFile).IsAssignableFrom(type))
            {
                yield return new PropInfo
                       {
                           Name         = "Files",
                           DefaultValue = "Array.Empty<ResourceModel>()",
                           Summary      = "附件",
                           TypeName     = "IEnumerable<ResourceModel>"
                       }
            }
            ;
        }