-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicExtension.cs
61 lines (55 loc) · 2.23 KB
/
DynamicExtension.cs
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CSharpExtensions.OpenSource
{
public static class DynamicExtension
{
public static dynamic GetNestedObj(dynamic obj, params string[] props)
{
dynamic res = obj;
foreach (var propName in props)
{
var item = JsonConvert.SerializeObject(res);
res = JsonConvert.DeserializeObject<IDictionary<string, dynamic>>(item);
res = res[propName];
}
return res;
}
public static void RemoveProp(dynamic item, string propName)
{
try
{
var dict = (IDictionary<string, dynamic>)item;
dict.Remove(propName);
}
catch { }
}
public static List<string> GetKeys(dynamic item)
{
if (item == null) { return new(); }
var dict = ((IDictionary<string, dynamic>)item);
return dict.Keys.ToList();
}
public static void SetProp(dynamic item, string propName, dynamic value) => ((IDictionary<string, dynamic>)item)[propName] = value;
public static dynamic? GetOrDefault(params Func<dynamic?>[] getValueMethods) => GetOrDefault<dynamic>(0, getValueMethods);
public static dynamic? GetOrDefault(int startedFunc = 0, params Func<dynamic?>[] getValueMethods) => GetOrDefault<dynamic>(startedFunc, getValueMethods);
public static T? GetOrDefault<T>(params Func<dynamic?>[] getValueMethods) => GetOrDefault<T>(0, getValueMethods);
public static T? GetOrDefault<T>(int startedFunc = 0, params Func<dynamic?>[] getValueMethods)
{
var jsonSettings = GenericsExtensions.GetJsonSerializerSettings(TypeNameHandling.None);
for (int i = startedFunc; i < getValueMethods.Length; i++)
{
try
{
var val = getValueMethods[i]();
var str = JsonConvert.SerializeObject(val, jsonSettings);
return JsonConvert.DeserializeObject<T>(str, jsonSettings);
}
catch { }
}
return default;
}
}
}