forked from dotnetcore/WebApiClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCeMetadata.cs
More file actions
191 lines (173 loc) · 6.1 KB
/
Copy pathCeMetadata.cs
File metadata and controls
191 lines (173 loc) · 6.1 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using Mono.Cecil;
using System;
using System.Linq;
using System.Reflection;
using System.Text;
namespace WebApiClient.BuildTask
{
/// <summary>
/// 表示cecil元数据抽象
/// </summary>
abstract class CeMetadata
{
/// <summary>
/// 所在程序集
/// </summary>
private readonly ModuleDefinition module;
/// <summary>
/// 所有已知类型
/// </summary>
private readonly TypeDefinition[] knowTypes;
/// <summary>
/// 获取系统类型
/// </summary>
public TypeSystem TypeSystem { get; private set; }
/// <summary>
/// cecil元数据抽象
/// </summary>
/// <param name="module">所在程序集</param>
public CeMetadata(CeAssembly assembly)
{
this.module = assembly.MainMdule;
this.knowTypes = assembly.KnowTypes;
this.TypeSystem = this.module.TypeSystem;
}
/// <summary>
/// 返回导入外部类型后的类型
/// </summary>
/// <typeparam name="T">目标类型</typeparam>
/// <exception cref="NotSupportedException"></exception>
/// <exception cref="TypeLoadException"></exception>
/// <returns></returns>
protected TypeReference ImportType<T>()
{
var type = typeof(T);
if (type.IsArray == true)
{
throw new NotSupportedException("不支持数组类型");
}
var knowType = this.knowTypes.FirstOrDefault(item => item.FullName == type.FullName);
if (knowType == null)
{
// 本程序集的类型不作直接导入
if (this.IsThisAssemblyType(type) == true)
{
throw new TypeLoadException($"找不到类型:{type.FullName}");
}
return this.module.ImportReference(type);
}
else
{
return this.module.ImportReference(knowType);
}
}
/// <summary>
/// 返回指定类型是在本程序集范围内
/// </summary>
/// <param name="type">类型</param>
/// <returns></returns>
private bool IsThisAssemblyType(Type type)
{
#if NETCOREAPP1_1
return type.GetTypeInfo().Assembly == this.GetType().GetTypeInfo().Assembly;
#else
return type.Assembly == this.GetType().Assembly;
#endif
}
/// <summary>
/// 返回导入外部类型的指定方法后的方法
/// </summary>
/// <param name="methodName">方法名</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="NotSupportedException"></exception>
/// <exception cref="TypeLoadException"></exception>
/// <returns></returns>
protected MethodReference ImportMethod<T>(string methodName)
{
return this.ImportMethod<T>(item => item.Name == methodName);
}
/// <summary>
/// 返回导入外部类型的指定方法后的方法
/// </summary>
/// <param name="filter">方法过滤器</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="NotSupportedException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="TypeLoadException"></exception>
/// <returns></returns>
protected MethodReference ImportMethod<T>(Func<MethodDefinition, bool> filter)
{
if (filter == null)
{
throw new ArgumentNullException(nameof(filter));
}
var method = this.ImportType<T>().Resolve().Methods.FirstOrDefault(filter);
if (method == null)
{
throw new ArgumentException("无法找到指定的方法");
}
return this.module.ImportReference(method);
}
/// <summary>
/// 比较两类型类型是一样
/// </summary>
/// <param name="source">类型</param>
/// <param name="target">目标类型</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
protected bool IsTypeEquals(TypeReference source, Type target)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
return source.FullName == target.FullName;
}
/// <summary>
/// 返回方法的完整名称
/// </summary>
/// <param name="method">方法</param>
/// <returns></returns>
protected string GetMethodFullName(MethodReference method)
{
var builder = new StringBuilder();
foreach (var p in method.Parameters)
{
if (builder.Length > 0)
{
builder.Append(",");
}
builder.Append(GetTypeName(p.ParameterType));
}
var insert = $"{GetTypeName(method.ReturnType)} {method.Name}(";
return builder.Insert(0, insert).Append(")").ToString();
}
/// <summary>
/// 返回类型不含namespace的名称
/// </summary>
/// <param name="type">类型</param>
/// <returns></returns>
private static string GetTypeName(TypeReference type)
{
if (type.IsGenericInstance == false)
{
return type.Name;
}
var builder = new StringBuilder();
var parameters = ((GenericInstanceType)type).GenericArguments;
foreach (var p in parameters)
{
if (builder.Length > 0)
{
builder.Append(",");
}
builder.Append(GetTypeName(p));
}
return builder.Insert(0, $"{type.Name}<").Append(">").ToString();
}
}
}