DictionaryExtensions.cs
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using NCC.Dependency;
using NCC.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
namespace NCC.ClayObject.Extensions
{
/// <summary>
/// 字典类型拓展类
/// </summary>
[SuppressSniffer]
public static class DictionaryExtensions
{
/// <summary>
/// 将对象转成字典
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static IDictionary<string, object> ToDictionary(this object input)
{
if (input == null) return default;
if (input is IDictionary<string, object> dictionary)
return dictionary;
if (input is Clay clay && clay.IsObject)
{
var dic = new Dictionary<string, object>();
foreach (KeyValuePair<string, dynamic> item in (dynamic)clay)
{
dic.Add(item.Key, item.Value is Clay v ? v.ToDictionary() : item.Value);
}
return dic;
}
if (input is JsonElement jsonElement && jsonElement.ValueKind == JsonValueKind.Object)
{
return jsonElement.ToObject() as IDictionary<string, object>;
}
var properties = input.GetType().GetProperties();
var fields = input.GetType().GetFields();
var members = properties.Cast<MemberInfo>().Concat(fields.Cast<MemberInfo>());
return members.ToDictionary(m => m.Name, m => GetValue(input, m));
}
/// <summary>
/// 将对象转字典类型,其中值返回原始类型 Type 类型
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static IDictionary<string, Tuple<Type, object>> ToDictionaryWithType(this object input)
{
if (input == null) return default;
if (input is IDictionary<string, object> dictionary)
return dictionary.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value == null ?
new Tuple<Type, object>(typeof(object), kvp.Value) :
new Tuple<Type, object>(kvp.Value.GetType(), kvp.Value)
);
var dict = new Dictionary<string, Tuple<Type, object>>();
// 获取所有属性列表
foreach (var property in input.GetType().GetProperties())
{
dict.Add(property.Name, new Tuple<Type, object>(property.PropertyType, property.GetValue(input, null)));
}
// 获取所有成员列表
foreach (var field in input.GetType().GetFields())
{
dict.Add(field.Name, new Tuple<Type, object>(field.FieldType, field.GetValue(input)));
}
return dict;
}
/// <summary>
/// 获取成员值
/// </summary>
/// <param name="obj"></param>
/// <param name="member"></param>
/// <returns></returns>
private static object GetValue(object obj, MemberInfo member)
{
if (member is PropertyInfo info)
return info.GetValue(obj, null);
if (member is FieldInfo info1)
return info1.GetValue(obj);
throw new ArgumentException("Passed member is neither a PropertyInfo nor a FieldInfo.");
}
/// <summary>
/// 获取指定键的值,不存在则按指定委托添加值
/// </summary>
/// <typeparam name="TKey">字典键类型</typeparam>
/// <typeparam name="TValue">字典值类型</typeparam>
/// <param name="dictionary">要操作的字典</param>
/// <param name="key">指定键名</param>
/// <param name="addFunc">添加值的委托</param>
/// <returns>获取到的值</returns>
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> addFunc)
{
if (dictionary.TryGetValue(key, out TValue value))
{
return value;
}
return dictionary[key] = addFunc();
}
}
}