mirror of https://github.com/abpframework/abp.git
5 changed files with 119 additions and 32 deletions
@ -1,36 +1,13 @@ |
|||||
using System; |
using System; |
||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.ComponentModel.DataAnnotations; |
|
||||
using Volo.Abp.Reflection; |
|
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending |
namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending |
||||
{ |
{ |
||||
[Serializable] |
[Serializable] |
||||
public class ExtensionPropertyAttributeDto |
public class ExtensionPropertyAttributeDto |
||||
{ |
{ |
||||
public string Type { get; set; } |
|
||||
public string TypeSimple { get; set; } |
public string TypeSimple { get; set; } |
||||
public Dictionary<string, object> Configuration { get; set; } |
|
||||
|
|
||||
public static ExtensionPropertyAttributeDto Create(Attribute attribute) |
public Dictionary<string, object> Config { get; set; } |
||||
{ |
|
||||
var attributeType = attribute.GetType(); |
|
||||
var dto = new ExtensionPropertyAttributeDto |
|
||||
{ |
|
||||
Type = TypeHelper.GetFullNameHandlingNullableAndGenerics(attributeType), |
|
||||
TypeSimple = TypeHelper.GetSimplifiedName(attributeType), |
|
||||
Configuration = new Dictionary<string, object>() |
|
||||
}; |
|
||||
|
|
||||
if (attribute is StringLengthAttribute stringLengthAttribute) |
|
||||
{ |
|
||||
dto.Configuration["MaximumLength"] = stringLengthAttribute.MaximumLength; |
|
||||
dto.Configuration["MinimumLength"] = stringLengthAttribute.MinimumLength; |
|
||||
} |
|
||||
|
|
||||
//TODO: Others!
|
|
||||
|
|
||||
return dto; |
|
||||
} |
|
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,95 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
using System.Reflection; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Reflection; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending |
||||
|
{ |
||||
|
public class ExtensionPropertyAttributeDtoFactory : IExtensionPropertyAttributeDtoFactory, ITransientDependency |
||||
|
{ |
||||
|
public virtual ExtensionPropertyAttributeDto Create(Attribute attribute) |
||||
|
{ |
||||
|
return new ExtensionPropertyAttributeDto |
||||
|
{ |
||||
|
TypeSimple = GetSimplifiedName(attribute), |
||||
|
Config = CreateConfiguration(attribute) |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
protected virtual string GetSimplifiedName(Attribute attribute) |
||||
|
{ |
||||
|
return attribute.GetType().Name.ToCamelCase().RemovePostFix("Attribute"); |
||||
|
} |
||||
|
|
||||
|
protected virtual Dictionary<string, object> CreateConfiguration(Attribute attribute) |
||||
|
{ |
||||
|
var configuration = new Dictionary<string, object>(); |
||||
|
|
||||
|
AddPropertiesToConfiguration(attribute, configuration); |
||||
|
|
||||
|
return configuration; |
||||
|
} |
||||
|
|
||||
|
protected virtual void AddPropertiesToConfiguration(Attribute attribute, Dictionary<string, object> configuration) |
||||
|
{ |
||||
|
var properties = attribute |
||||
|
.GetType() |
||||
|
.GetProperties(BindingFlags.Instance | BindingFlags.Public); |
||||
|
|
||||
|
foreach (var property in properties) |
||||
|
{ |
||||
|
if (IgnoreProperty(attribute, property)) |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
var value = GetPropertyValue(attribute, property); |
||||
|
if (value == null) |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
configuration[property.Name.ToCamelCase()] = value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual bool IgnoreProperty(Attribute attribute, PropertyInfo property) |
||||
|
{ |
||||
|
if (property.DeclaringType == null || |
||||
|
property.DeclaringType.IsIn(typeof(ValidationAttribute), typeof(Attribute), typeof(object))) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
if (property.PropertyType == typeof(DisplayFormatAttribute)) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
protected virtual object GetPropertyValue(Attribute attribute, PropertyInfo property) |
||||
|
{ |
||||
|
var value = property.GetValue(attribute); |
||||
|
if (value == null) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
if (property.PropertyType.IsEnum) |
||||
|
{ |
||||
|
return Enum.GetName(property.PropertyType, value); |
||||
|
} |
||||
|
|
||||
|
if (property.PropertyType == typeof(Type)) |
||||
|
{ |
||||
|
return TypeHelper.GetSimplifiedName((Type) value); |
||||
|
} |
||||
|
|
||||
|
return value; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending |
||||
|
{ |
||||
|
public interface IExtensionPropertyAttributeDtoFactory |
||||
|
{ |
||||
|
ExtensionPropertyAttributeDto Create(Attribute attribute); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue