@@ -1,5 +1,7 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace IRaCIS.Core.Infrastructure.Extention
|
||||
{
|
||||
@@ -38,7 +40,30 @@ namespace IRaCIS.Core.Infrastructure.Extention
|
||||
public static string ToJsonNotIgnoreNull(this object obj)
|
||||
{
|
||||
|
||||
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", ReferenceLoopHandling = ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Include });
|
||||
return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", Formatting = Formatting.Indented, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Include });
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<string, object> ConvertToDictionary(this object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
}
|
||||
|
||||
Type type = obj.GetType();
|
||||
PropertyInfo[] properties = type.GetProperties();
|
||||
|
||||
Dictionary<string, object> dictionary = new Dictionary<string, object>();
|
||||
|
||||
foreach (PropertyInfo property in properties)
|
||||
{
|
||||
string propertyName = property.Name;
|
||||
object propertyValue = property.GetValue(obj);
|
||||
|
||||
dictionary.Add(propertyName, propertyValue);
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user