-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathConvertUtil.cs
More file actions
165 lines (150 loc) · 5.67 KB
/
Copy pathConvertUtil.cs
File metadata and controls
165 lines (150 loc) · 5.67 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using PaySharp.Core;
using PaySharp.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace PaySharp.Wechatpay
{
internal static class ConvertUtil
{
public static List<T> ToList<T, TChildren>(GatewayData gatewayData, int index) where T:new() where TChildren:new()
{
var flag = true;
var list = new List<T>();
int i = 0;
while (flag)
{
var type = typeof(T);
var obj = new T();
var properties = type.GetProperties();
var isFirstProperty = true;
foreach (var item in properties)
{
if (item.PropertyType == typeof(List<TChildren>))
{
var chidrenList = ToList<TChildren, object>(gatewayData, i);
item.SetValue(obj, chidrenList);
continue;
}
string key = GetRealName(item);
if (index > -1)
{
key += $"_{index}";
}
key += $"_{i}";
var value = gatewayData.GetStringValue(key);
if (value == null)
{
if (isFirstProperty)
{
flag = false;
break;
}
continue;
}
isFirstProperty = false;
item.SetValue(obj, Convert.ChangeType(value, item.PropertyType));
}
if (!flag)
{
return list;
}
list.Add((T)obj);
i++;
}
return list;
}
/// <summary>
/// 获取字段json中的名字
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
private static string GetRealName(System.Reflection.PropertyInfo item)
{
string key;
var renameAttribute = item.GetCustomAttributes(typeof(ReNameAttribute), true);
if (renameAttribute.Length > 0)
{
key = ((ReNameAttribute)renameAttribute[0]).Name;
}
else
{
key = item.Name.ToSnakeCase();
}
return key;
}
/// <summary>
/// 将微信返回值特殊格式转化为列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="gatewayData"></param>
/// <returns></returns>
public static List<T> ToList<T>(GatewayData gatewayData) where T : new()
{
var list = new List<T>();
var properties = typeof(T).GetProperties();
var keyfirst = properties[0];
var count = gatewayData.Keys.Where(p => Regex.IsMatch(p, $@"{GetRealName(keyfirst)}_\d")).Count();
for (var i = 0; i < count; i++)
{
var item = new T();
foreach(var field in properties)
{
string keyname = $"{GetRealName(field)}_{i}";
field.SetValue(item, gatewayData.GetStringValue(keyname));
}
list.Add(item);
}
return list;
}
/// <summary>
/// 将微信返回值特殊格式转化为列表(二级)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TChildren"></typeparam>
/// <param name="gatewayData"></param>
/// <returns></returns>
public static List<T> ToList<T, TChildren>(GatewayData gatewayData) where T : new() where TChildren : new()
{
var list = new List<T>();
var properties = typeof(T).GetProperties();
var keyfirst = properties[0];
var count = gatewayData.Keys.Where(p => Regex.IsMatch(p, $@"{GetRealName(keyfirst)}_\d")).Count();
for (var i = 0; i < count; i++)
{
var item = new T();
foreach (var field in properties)
{
if (field.PropertyType == typeof(List<TChildren>))
{
var sublist = new List<TChildren>();
var subProperties = typeof(TChildren).GetProperties();
var subFirstkey = subProperties[0];
var SubFirstName = GetRealName(subFirstkey);
var subCount = gatewayData.Keys.Where(p => Regex.IsMatch(p, $@"{SubFirstName}_{i}_\d")).Count();
for(var j = 0; j < subCount; j++)
{
var subItem = new TChildren();
foreach (var subfield in subProperties)
{
string keyname = $"{GetRealName(subfield)}_{i}_{j}";
subfield.SetValue(subItem, gatewayData.GetStringValue(keyname));
}
sublist.Add(subItem);
}
field.SetValue(item, sublist);
}
else
{
string keyname = $"{GetRealName(field)}_{i}";
field.SetValue(item, gatewayData.GetStringValue(keyname));
}
}
list.Add(item);
}
return list;
}
}
}