diff --git a/lib/ApiMultipartFormDataFormatter/Services/Implementations/BaseMultiPartFormdataModelBinderService.cs b/lib/ApiMultipartFormDataFormatter/Services/Implementations/BaseMultiPartFormdataModelBinderService.cs index 140313e..663779e 100644 --- a/lib/ApiMultipartFormDataFormatter/Services/Implementations/BaseMultiPartFormdataModelBinderService.cs +++ b/lib/ApiMultipartFormDataFormatter/Services/Implementations/BaseMultiPartFormdataModelBinderService.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using System.Linq; using System.Reflection; using ApiMultiPartFormData.Services.Interfaces; @@ -23,14 +23,46 @@ public object BuildModel(PropertyInfo propertyInfo, object value) // Get property type. var propertyType = propertyInfo.PropertyType; + var underlyingType = Nullable.GetUnderlyingType(propertyType); // Property is GUID. if (propertyType == typeof(Guid) && Guid.TryParse(value.ToString(), out var guid)) return guid; + if (underlyingType == typeof(Guid)) + { + if (Guid.TryParse(value.ToString(), out guid)) + return guid; + return null; + } + + // Property is Enum. + if (propertyType.IsEnum) + return convertToEnum(propertyType, value.ToString()); + if (underlyingType != null && underlyingType.IsEnum) + { + if (string.IsNullOrWhiteSpace(value.ToString())) + return null; + return convertToEnum(underlyingType, value.ToString()); + } + + // Other Nullable types + if (underlyingType != null) + { + if (string.IsNullOrEmpty(value.ToString())) return null; + propertyType = underlyingType; + } return Convert.ChangeType(value, propertyType); } + object convertToEnum(Type type, string val) + { + if (int.TryParse(val, out var num)) + return Enum.ToObject(type, num); + + return Enum.Parse(type, val, true); + } + #endregion } } \ No newline at end of file