using System.Collections.Generic; using System.ComponentModel; using System.Linq; namespace BlueWest.DataAgent { public static class DataExtensions { /*public static Dictionary ToDictionary(this object arg) { var dic = arg.GetType().GetProperties() .ToDictionary(property => property.Name, property => property.GetValue(arg)); return dic; }*/ public static Dictionary ToDictionaryStr(this object source) { Dictionary result = new Dictionary(); string[] keys = { }; string[] values = { }; bool outLoopingKeys = false, outLoopingValues = false; foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source)) { object value = property.GetValue(source).ToString(); if (value is Dictionary.KeyCollection keyCollection) { keys = keyCollection.ToArray(); outLoopingKeys = true; } if (value is Dictionary.ValueCollection valueCollection) { values = valueCollection.ToArray(); outLoopingValues = true; } if(outLoopingKeys & outLoopingValues) { break; } } for (int i = 0; i < keys.Length; i++) { result.Add(keys[i].ToString(), values[i].ToString()); } return result; } } }