2021-12-06 02:49:27 +03:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace BlueWest.DataAgent
|
|
|
|
|
{
|
|
|
|
|
public static class DataExtensions
|
|
|
|
|
{
|
|
|
|
|
/*public static Dictionary<string, object> ToDictionary(this object arg)
|
|
|
|
|
{
|
|
|
|
|
var dic = arg.GetType().GetProperties()
|
|
|
|
|
.ToDictionary(property => property.Name, property => property.GetValue(arg));
|
|
|
|
|
return dic;
|
|
|
|
|
}*/
|
|
|
|
|
public static Dictionary<string,string> ToDictionaryStr(this object source)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
string[] keys = { };
|
|
|
|
|
string[] values = { };
|
|
|
|
|
|
|
|
|
|
bool outLoopingKeys = false, outLoopingValues = false;
|
|
|
|
|
|
|
|
|
|
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
|
|
|
|
|
{
|
2022-09-10 00:33:17 +03:00
|
|
|
|
object? value = property.GetValue(source)?.ToString();
|
2021-12-06 02:49:27 +03:00
|
|
|
|
|
|
|
|
|
if (value is Dictionary<string, string>.KeyCollection keyCollection)
|
|
|
|
|
{
|
|
|
|
|
keys = keyCollection.ToArray();
|
|
|
|
|
outLoopingKeys = true;
|
|
|
|
|
}
|
|
|
|
|
if (value is Dictionary<string, string>.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|