53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
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))
|
|
{
|
|
object value = property.GetValue(source).ToString();
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |