Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ApiMultiPartFormData.Services.Interfaces;

Expand All @@ -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
}
}