Browse Source

Improve extension property model binding.

pull/3961/head
Halil İbrahim Kalkan 6 years ago
parent
commit
25085b2959
  1. 4
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpExtraPropertyModelBinder.cs
  2. 36
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/ObjectExtending/ObjectExtendingPropertyInfoExtensions.cs
  3. 13
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs
  4. 8
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs

4
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpExtraPropertyModelBinder.cs

@ -1,7 +1,9 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Reflection;
namespace Volo.Abp.AspNetCore.Mvc.ModelBinding
{
@ -59,7 +61,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ModelBinding
return value;
}
return Convert.ChangeType(value, propertyInfo.Type);
return TypeHelper.ConvertFromString(propertyInfo.Type, value);
}
}
}

36
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/ObjectExtending/ObjectExtendingPropertyInfoExtensions.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
@ -6,6 +7,31 @@ namespace Volo.Abp.ObjectExtending
{
public static class ObjectExtensionPropertyInfoAspNetCoreMvcExtensions
{
private static readonly HashSet<Type> NumberTypes = new HashSet<Type> {
typeof(int),
typeof(long),
typeof(byte),
typeof(sbyte),
typeof(short),
typeof(ushort),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(int?),
typeof(long?),
typeof(byte?),
typeof(sbyte?),
typeof(short?),
typeof(ushort?),
typeof(uint?),
typeof(long?),
typeof(ulong?),
typeof(float?),
typeof(double?),
};
public static string GetInputType(this ObjectExtensionPropertyInfo propertyInfo)
{
foreach (var attribute in propertyInfo.Attributes)
@ -79,15 +105,7 @@ namespace Volo.Abp.ObjectExtending
return "datetime-local";
}
if (type == typeof(int) ||
type == typeof(long) ||
type == typeof(byte) ||
type == typeof(sbyte) ||
type == typeof(short) ||
type == typeof(ushort) ||
type == typeof(uint) ||
type == typeof(long) ||
type == typeof(ulong))
if (NumberTypes.Contains(type))
{
return "number";
}

13
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
@ -264,5 +265,17 @@ namespace Volo.Abp.Reflection
return type.FullName;
}
public static object ConvertFromString<TTargetType>(string value)
{
return ConvertFromString(typeof(TTargetType), value);
}
public static object ConvertFromString(Type targetType, string value)
{
return TypeDescriptor
.GetConverter(targetType)
.ConvertFromString(value);
}
}
}

8
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs

@ -75,6 +75,14 @@ namespace Volo.Abp.Reflection
TypeHelper.GetDefaultValue(typeof(string)).ShouldBeNull();
}
[Fact]
public void ConvertFromString()
{
TypeHelper.ConvertFromString<int>("42").ShouldBe(42);
TypeHelper.ConvertFromString<int?>("42").ShouldBe((int?)42);
TypeHelper.ConvertFromString<int?>(null).ShouldBeNull();
}
public class MyDictionary : Dictionary<bool, TypeHelper_Tests>
{

Loading…
Cancel
Save