obj to dic props
Anonymous
plain_text
03/14/2023 8:39 PM
1020 B
11
Indexable
public static Dictionary<string, object> ObjectToDictionary(object obj)
{
var dict = new Dictionary<string, object>();
var properties = obj.GetType().GetProperties();
foreach (var property in properties)
{
var value = property.GetValue(obj);
if (value == null)
{
dict.Add(property.Name, null);
}
else if (value.GetType().IsPrimitive || value is string)
{
dict.Add(property.Name, value);
}
else
{
var nestedDict = ObjectToDictionary(value);
foreach (var kvp in nestedDict)
{
dict.Add($"{property.Name}.{kvp.Key}", kvp.Value);
}
}
}
return dict;
}
Editor is loading...