Code: Select all
public IEnumerable ToKeyValuePairs(object obj)
{
List keyValuePairs = new();
IEnumerable
props = obj.GetType().GetProperties()
.Where(p => p.GetValue(obj, null) != null);
foreach (PropertyInfo p in props)
{
object value = p.GetValue(obj, null);
ICollection collection = value as ICollection;
if (collection != null)
{
AddRange(p.Name, ...???...)); // How to
// cast `ICollection collection` to `IEnumerable`?
}
else
{
keyValuePairs.Add(
new(p.Name, value.ToString()));
}
}
return keyValuePairs;
}
Code: Select all
public List AddRange(
string keyName,
IEnumerable arrayElements)
{
// ... the code is omitted for the brevity
}
Mobile version