Add ToObject<T> extension method to Dictionary

This commit is contained in:
ChronosX88 2020-10-08 21:08:29 +04:00
parent 9e0088cbba
commit 42a5ffba05
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A

View File

@ -27,7 +27,7 @@ namespace Zirconium.Utils
private static void AddPropertyToDictionary<T>(PropertyDescriptor property, object source, Dictionary<string, T> dictionary) private static void AddPropertyToDictionary<T>(PropertyDescriptor property, object source, Dictionary<string, T> dictionary)
{ {
object value = property.GetValue(source); object value = property.GetValue(source);
if (IsOfType<T>(value)) if (value is T)
{ {
var jsonProp = property.Attributes.OfType<JsonPropertyAttribute>().FirstOrDefault(); var jsonProp = property.Attributes.OfType<JsonPropertyAttribute>().FirstOrDefault();
string propName; string propName;
@ -39,13 +39,39 @@ namespace Zirconium.Utils
{ {
propName = jsonProp.PropertyName; propName = jsonProp.PropertyName;
} }
if (jsonProp.Required == Required.Default && value == null){
return;
}
dictionary.Add(propName, (T)value); dictionary.Add(propName, (T)value);
} }
} }
private static bool IsOfType<T>(object value) public static T ToObject<T>(this IDictionary<string, object> source)
where T : class, new()
{ {
return value is T; var someObject = new T();
var someObjectType = someObject.GetType();
foreach (var item in source)
{
string propName = null;
foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(someObject)) {
var jsonProp = prop.Attributes.OfType<JsonPropertyAttribute>().FirstOrDefault();
if (jsonProp != null) {
if (jsonProp.PropertyName == item.Key) {
propName = prop.Name;
}
}
}
if (propName == null) {
propName = item.Key;
}
someObjectType
.GetProperty(propName)
.SetValue(someObject, item.Value, null);
}
return someObject;
} }
private static void ThrowExceptionWhenSourceArgumentIsNull() private static void ThrowExceptionWhenSourceArgumentIsNull()