4
4
using MSAddinTest . Utils ;
5
5
using System ;
6
6
using System . Collections . Generic ;
7
+ using System . Globalization ;
7
8
using System . IO ;
8
9
using System . Linq ;
9
10
using System . Reflection ;
@@ -46,23 +47,34 @@ public PluginAssemblyLoader(LoaderSetup pluginDomainSetup)
46
47
private List < ExecutorBase > _executors = new List < ExecutorBase > ( ) ;
47
48
48
49
private string _lastFileHash = "" ;
50
+ private Assembly _currentAssembly ;
49
51
public FuncResult LoadAssembly ( )
50
52
{
51
53
try
52
54
{
55
+ // 判断文件是否存在
56
+ if ( ! File . Exists ( Setup . DllFullPath ) )
57
+ {
58
+ return new FuncResult ( false , "文件不存在" ) ;
59
+ }
60
+
53
61
// 验证文件 hash 值
54
- var newFileHash = FileHelper . GetFileHash ( Setup . DllFullPath ) ;
55
- if ( _lastFileHash == newFileHash )
56
- return new FuncResult ( false , "文件未改变" ) ;
57
- else
58
- _lastFileHash = newFileHash ;
62
+ //var newFileHash = FileHelper.GetFileHash(Setup.DllFullPath);
63
+ //if (_lastFileHash == newFileHash)
64
+ // return new FuncResult(false, "文件未改变");
65
+ //else
66
+ // _lastFileHash = newFileHash;
67
+
68
+ // 执行卸载逻辑
69
+ _msAddins . ForEach ( x => x . Unloaded ( ) ) ;
70
+ _msAddins . Clear ( ) ;
59
71
60
72
// 读取文件然后加载
61
73
byte [ ] bytes = File . ReadAllBytes ( Setup . DllFullPath ) ;
62
- var assembly = Assembly . Load ( bytes ) ;
74
+ _currentAssembly = Assembly . Load ( bytes ) ;
63
75
64
- var results = BuilderExecutors ( assembly ) ;
65
- _executors = results . ToList ( ) ;
76
+ _executors . Clear ( ) ;
77
+ BuilderExecutors ( _currentAssembly ) ;
66
78
67
79
return new FuncResult ( true ) ;
68
80
}
@@ -72,8 +84,6 @@ public FuncResult LoadAssembly()
72
84
}
73
85
}
74
86
75
-
76
-
77
87
/// <summary>
78
88
/// 生成执行器
79
89
/// </summary>
@@ -82,18 +92,31 @@ protected virtual IEnumerable<ExecutorBase> BuilderExecutors(Assembly assembly)
82
92
{
83
93
var results = new List < ExecutorBase > ( ) ;
84
94
85
- // 获取类执行器
86
- results . AddRange ( GenerateClassExecutor ( assembly ) ) ;
95
+ AddExecutors ( GenerateClassExecutor ( assembly ) ) ;
87
96
88
97
// 静态方法执行器
89
- results . AddRange ( GenerateStaticMethodExecutor ( assembly ) ) ;
98
+ AddExecutors ( GenerateStaticMethodExecutor ( assembly ) ) ;
90
99
91
100
// 添加 addin 执行器
92
- results . AddRange ( GenerateAddinExecutor ( assembly ) ) ;
101
+ AddExecutors ( GenerateAddinExecutor ( assembly ) ) ;
93
102
94
103
return results ;
95
104
}
96
105
106
+ private void AddExecutors ( IEnumerable < ExecutorBase > executors )
107
+ {
108
+ foreach ( var executor in executors )
109
+ {
110
+ var executorTemp = _executors . Find ( x => x . IsSame ( executor ) ) ;
111
+ // 如果没找到或者优先级比原来高,则添加
112
+ if ( executorTemp == null || executorTemp . Priority < executor . Priority )
113
+ {
114
+ _executors . Remove ( executorTemp ) ;
115
+ _executors . Add ( executor ) ;
116
+ }
117
+ }
118
+ }
119
+
97
120
// 获取类执行器
98
121
private IEnumerable < ExecutorBase > GenerateClassExecutor ( Assembly assembly )
99
122
{
@@ -137,9 +160,9 @@ private IEnumerable<ExecutorBase> GenerateStaticMethodExecutor(Assembly assembly
137
160
{
138
161
// 获取参数
139
162
var paraInfos = methodInfo . GetParameters ( ) ;
140
- if ( paraInfos . Length != 1 || ! typeof ( IMSTestArg ) . IsAssignableFrom ( paraInfos [ 0 ] . ParameterType ) )
163
+ if ( paraInfos . Length != 1 || ! typeof ( string ) . IsAssignableFrom ( paraInfos [ 0 ] . ParameterType ) )
141
164
{
142
- MessageCenter . Instance . ShowDebugMessage ( $ "静态方法 { methodInfo . Name } 的参数个数必须有且只有一个 IMSTestArg 参数", "" , false ) ;
165
+ MessageCenter . Instance . ShowDebugMessage ( $ "静态方法 { methodInfo . Name } 的参数个数必须有且只有一个 string 参数", "" , false ) ;
143
166
continue ;
144
167
} ;
145
168
@@ -153,6 +176,7 @@ private IEnumerable<ExecutorBase> GenerateStaticMethodExecutor(Assembly assembly
153
176
return results ;
154
177
}
155
178
179
+ private List < MSTest_Addin > _msAddins = new List < MSTest_Addin > ( ) ;
156
180
// 读取addin执行器
157
181
private IEnumerable < ExecutorBase > GenerateAddinExecutor ( Assembly assembly )
158
182
{
@@ -166,9 +190,14 @@ private IEnumerable<ExecutorBase> GenerateAddinExecutor(Assembly assembly)
166
190
// 找到后立即进行初始化
167
191
try
168
192
{
169
- var addin = Activator . CreateInstance ( pluginType , IntPtr . Zero ) as MSTest_Addin ;
193
+ var addin = Activator . CreateInstance ( pluginType ,
194
+ BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ,
195
+ null ,
196
+ new object [ ] { IntPtr . Zero } , CultureInfo . CurrentCulture ) as MSTest_Addin ;
197
+
170
198
// 进行初始化
171
199
addin . Init ( Index . MSAddin . Instance ) ;
200
+ _msAddins . Add ( addin ) ;
172
201
}
173
202
catch ( Exception ex )
174
203
{
0 commit comments