-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActionReplaceOption.cs
81 lines (67 loc) · 2.9 KB
/
ActionReplaceOption.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using Microsoft.AspNetCore.Mvc.Controllers;
using System.Reflection;
namespace EasyOC.ReplaceAction
{
public class ActionReplaceOption
{
public List<ActionReplaceOptionItem> Items { get; set; } = new List<ActionReplaceOptionItem>();
public List<ActionReplaceOptionItem> AddReplaceOption<TargetNew>(string targetControllerName, IDictionary<string, string> actionMapping, int order = 0)
where TargetNew : class
{
var type = typeof(TargetNew);
var typeInfo = type.GetTypeInfo();
var _actionMapping = actionMapping.ToDictionary(k => k.Key, v => type.GetMethod(actionMapping[v.Key]));
Items.Add(new ActionReplaceOptionItem
{
TargetControllerFullName = targetControllerName,
NewController = type,
ActionMapping = _actionMapping
});
return Items;
}
public List<ActionReplaceOptionItem> AddReplaceOption<TargetNew>(string targetControllerName, IDictionary<MethodDescription, MethodDescription> actionMapping)
where TargetNew : class
{
var type = typeof(TargetNew);
var typeInfo = type.GetTypeInfo();
foreach (var kv in actionMapping)
{
var newMethod = kv.Value.Parameters != null ? typeInfo.GetMethod(kv.Value.Name, kv.Value.Parameters) :
typeInfo.GetMethod(kv.Value.Name);
if (newMethod != null)
{
var item = new ActionReplaceOptionItem
{
TargetControllerFullName = targetControllerName,
NewController = type,
TargetMethodDescriptions = new KeyValuePair<MethodDescription, MethodInfo>(kv.Key, newMethod)
};
Items.Add(item);
}
}
return Items;
}
public List<ActionReplaceOptionItem> AddReplaceOption<SourceOld, TargetNew>(IDictionary<string, string> ActionMapping, int order = 0)
where TargetNew : class
{
return AddReplaceOption<TargetNew>(typeof(SourceOld).FullName, ActionMapping, order);
}
}
public class MethodDescription
{
public string Name { get; set; }
/// <summary>
/// 如果未指定参数则不检查
/// </summary>
public Type[] Parameters { get; set; }
}
public class ActionReplaceOptionItem
{
public int Order { get; set; } = 0;
public Dictionary<string, MethodInfo> ActionMapping { get; set; }
public KeyValuePair<MethodDescription, MethodInfo>? TargetMethodDescriptions { get; set; }
public Type NewController { get; set; }
public string TargetControllerFullName { get; set; }
public Action<ControllerActionDescriptor> CustomAction { get; set; }
}
}