@@ -12,6 +12,17 @@ namespace WebApiClientCore
1212 /// <typeparam name="THttpApi"></typeparam>
1313 class HttpApiEmitActivator < THttpApi > : HttpApiActivator < THttpApi >
1414 {
15+ /// <summary>
16+ /// IActionInterceptor的Intercept方法
17+ /// </summary>
18+ private static readonly MethodInfo interceptMethod = typeof ( IActionInterceptor ) . GetMethod ( nameof ( IActionInterceptor . Intercept ) ) ?? throw new MissingMethodException ( nameof ( IActionInterceptor . Intercept ) ) ;
19+
20+ /// <summary>
21+ /// 代理类型的构造器的参数类型
22+ /// (IApiInterceptor interceptor,IActionInvoker[] actionInvokers)
23+ /// </summary>
24+ private static readonly Type [ ] proxyTypeCtorArgTypes = new Type [ ] { typeof ( IActionInterceptor ) , typeof ( IActionInvoker [ ] ) } ;
25+
1526 /// <summary>
1627 /// 创建实例工厂
1728 /// </summary>
@@ -21,171 +32,155 @@ class HttpApiEmitActivator<THttpApi> : HttpApiActivator<THttpApi>
2132 protected override Func < IActionInterceptor , THttpApi > CreateFactory ( )
2233 {
2334 var actionInvokers = this . GetActionInvokers ( ) ;
24- var proxyType = ProxyTypeBuilder . Build ( typeof ( THttpApi ) , actionInvokers ) ;
35+ var proxyType = BuildProxyType ( typeof ( THttpApi ) , actionInvokers ) ;
2536 var proxyTypeCtor = Lambda . CreateCtorFunc < IActionInterceptor , IActionInvoker [ ] , THttpApi > ( proxyType ) ;
2637 return ( interceptor ) => proxyTypeCtor ( interceptor , actionInvokers ) ;
2738 }
2839
40+
2941 /// <summary>
30- /// 提供IHttpApi代理类的类型创建
42+ /// 创建IHttpApi代理类的类型
3143 /// </summary>
32- private static class ProxyTypeBuilder
44+ /// <param name="interfaceType">接口类型</param>
45+ /// <param name="actionInvokers">action执行器</param>
46+ /// <exception cref="NotSupportedException"></exception>
47+ /// <exception cref="ProxyTypeCreateException"></exception>
48+ /// <returns></returns>
49+ public static Type BuildProxyType ( Type interfaceType , IActionInvoker [ ] actionInvokers )
3350 {
34- /// <summary>
35- /// IActionInterceptor的Intercept方法
36- /// </summary>
37- private static readonly MethodInfo interceptMethod = typeof ( IActionInterceptor ) . GetMethod ( nameof ( IActionInterceptor . Intercept ) ) ?? throw new MissingMethodException ( nameof ( IActionInterceptor . Intercept ) ) ;
38-
39- /// <summary>
40- /// 代理类型的构造器的参数类型
41- /// (IApiInterceptor interceptor,IActionInvoker[] actionInvokers)
42- /// </summary>
43- private static readonly Type [ ] proxyTypeCtorArgTypes = new Type [ ] { typeof ( IActionInterceptor ) , typeof ( IActionInvoker [ ] ) } ;
44-
45- /// <summary>
46- /// 创建IHttpApi代理类的类型
47- /// </summary>
48- /// <param name="interfaceType">接口类型</param>
49- /// <param name="actionInvokers">action执行器</param>
50- /// <exception cref="NotSupportedException"></exception>
51- /// <exception cref="ProxyTypeCreateException"></exception>
52- /// <returns></returns>
53- public static Type Build ( Type interfaceType , IActionInvoker [ ] actionInvokers )
51+ // 接口的实现在动态程序集里,所以接口必须为public修饰才可以创建代理类并实现此接口
52+ if ( interfaceType . IsVisible == false )
5453 {
55- // 接口的实现在动态程序集里,所以接口必须为public修饰才可以创建代理类并实现此接口
56- if ( interfaceType . IsVisible == false )
57- {
58- var message = Resx . required_PublicInterface . Format ( interfaceType ) ;
59- throw new NotSupportedException ( message ) ;
60- }
54+ var message = Resx . required_PublicInterface . Format ( interfaceType ) ;
55+ throw new NotSupportedException ( message ) ;
56+ }
6157
62- var moduleName = interfaceType . Module . Name ;
63- var assemblyName = new AssemblyName ( Guid . NewGuid ( ) . ToString ( ) ) ;
58+ var moduleName = interfaceType . Module . Name ;
59+ var assemblyName = new AssemblyName ( Guid . NewGuid ( ) . ToString ( ) ) ;
6460
65- var module = AssemblyBuilder
66- . DefineDynamicAssembly ( assemblyName , AssemblyBuilderAccess . Run )
67- . DefineDynamicModule ( moduleName ) ;
61+ var module = AssemblyBuilder
62+ . DefineDynamicAssembly ( assemblyName , AssemblyBuilderAccess . Run )
63+ . DefineDynamicModule ( moduleName ) ;
6864
69- var typeName = interfaceType . FullName ?? Guid . NewGuid ( ) . ToString ( ) ;
70- var builder = module . DefineType ( typeName , TypeAttributes . Class ) ;
71- builder . AddInterfaceImplementation ( interfaceType ) ;
65+ var typeName = interfaceType . FullName ?? Guid . NewGuid ( ) . ToString ( ) ;
66+ var builder = module . DefineType ( typeName , TypeAttributes . Class ) ;
67+ builder . AddInterfaceImplementation ( interfaceType ) ;
7268
73- var fieldActionInterceptor = BuildField ( builder , "<>actionInterceptor" , typeof ( IActionInterceptor ) ) ;
74- var fieldActionInvokers = BuildField ( builder , "<>actionInvokers" , typeof ( IActionInvoker [ ] ) ) ;
69+ var fieldActionInterceptor = BuildField ( builder , "<>actionInterceptor" , typeof ( IActionInterceptor ) ) ;
70+ var fieldActionInvokers = BuildField ( builder , "<>actionInvokers" , typeof ( IActionInvoker [ ] ) ) ;
7571
76- BuildCtor ( builder , fieldActionInterceptor , fieldActionInvokers ) ;
77- BuildMethods ( builder , actionInvokers , fieldActionInterceptor , fieldActionInvokers ) ;
72+ BuildCtor ( builder , fieldActionInterceptor , fieldActionInvokers ) ;
73+ BuildMethods ( builder , actionInvokers , fieldActionInterceptor , fieldActionInvokers ) ;
7874
79- var proxyType = builder . CreateType ( ) ;
80- return proxyType ?? throw new ProxyTypeCreateException ( interfaceType ) ;
81- }
75+ var proxyType = builder . CreateType ( ) ;
76+ return proxyType ?? throw new ProxyTypeCreateException ( interfaceType ) ;
77+ }
8278
83- /// <summary>
84- /// 生成代理类型的字段
85- /// </summary>
86- /// <param name="builder">类型生成器</param>
87- /// <param name="fieldName">字段名称</param>
88- /// <param name="fieldType">字段类型</param>
89- /// <returns></returns>
90- private static FieldBuilder BuildField ( TypeBuilder builder , string fieldName , Type fieldType )
91- {
92- const FieldAttributes filedAttribute = FieldAttributes . Private | FieldAttributes . InitOnly ;
93- return builder . DefineField ( fieldName , fieldType , filedAttribute ) ;
94- }
79+ /// <summary>
80+ /// 生成代理类型的字段
81+ /// </summary>
82+ /// <param name="builder">类型生成器</param>
83+ /// <param name="fieldName">字段名称</param>
84+ /// <param name="fieldType">字段类型</param>
85+ /// <returns></returns>
86+ private static FieldBuilder BuildField ( TypeBuilder builder , string fieldName , Type fieldType )
87+ {
88+ const FieldAttributes filedAttribute = FieldAttributes . Private | FieldAttributes . InitOnly ;
89+ return builder . DefineField ( fieldName , fieldType , filedAttribute ) ;
90+ }
9591
96- /// <summary>
97- /// 生成代理类型的构造器
98- /// </summary>
99- /// <param name="builder">类型生成器</param>
100- /// <param name="fieldActionInterceptor">拦截器字段</param>
101- /// <param name="fieldActionInvokers">action执行器字段</param>
102- /// <returns></returns>
103- private static void BuildCtor ( TypeBuilder builder , FieldBuilder fieldActionInterceptor , FieldBuilder fieldActionInvokers )
104- {
105- // .ctor(IApiInterceptor actionInterceptor, IActionInvoker[] actionInvokers)
106- var ctor = builder . DefineConstructor ( MethodAttributes . Public , CallingConventions . Standard , proxyTypeCtorArgTypes ) ;
92+ /// <summary>
93+ /// 生成代理类型的构造器
94+ /// </summary>
95+ /// <param name="builder">类型生成器</param>
96+ /// <param name="fieldActionInterceptor">拦截器字段</param>
97+ /// <param name="fieldActionInvokers">action执行器字段</param>
98+ /// <returns></returns>
99+ private static void BuildCtor ( TypeBuilder builder , FieldBuilder fieldActionInterceptor , FieldBuilder fieldActionInvokers )
100+ {
101+ // .ctor(IApiInterceptor actionInterceptor, IActionInvoker[] actionInvokers)
102+ var ctor = builder . DefineConstructor ( MethodAttributes . Public , CallingConventions . Standard , proxyTypeCtorArgTypes ) ;
107103
108- var il = ctor . GetILGenerator ( ) ;
104+ var il = ctor . GetILGenerator ( ) ;
109105
110- // this.actionInterceptor = 第一个参数
111- il . Emit ( OpCodes . Ldarg_0 ) ;
112- il . Emit ( OpCodes . Ldarg_1 ) ;
113- il . Emit ( OpCodes . Stfld , fieldActionInterceptor ) ;
106+ // this.actionInterceptor = 第一个参数
107+ il . Emit ( OpCodes . Ldarg_0 ) ;
108+ il . Emit ( OpCodes . Ldarg_1 ) ;
109+ il . Emit ( OpCodes . Stfld , fieldActionInterceptor ) ;
114110
115- // this.actionInvokers = 第二个参数
116- il . Emit ( OpCodes . Ldarg_0 ) ;
117- il . Emit ( OpCodes . Ldarg_2 ) ;
118- il . Emit ( OpCodes . Stfld , fieldActionInvokers ) ;
111+ // this.actionInvokers = 第二个参数
112+ il . Emit ( OpCodes . Ldarg_0 ) ;
113+ il . Emit ( OpCodes . Ldarg_2 ) ;
114+ il . Emit ( OpCodes . Stfld , fieldActionInvokers ) ;
119115
120- il . Emit ( OpCodes . Ret ) ;
121- }
116+ il . Emit ( OpCodes . Ret ) ;
117+ }
122118
123- /// <summary>
124- /// 生成代理类型的接口实现方法
125- /// </summary>
126- /// <param name="builder">类型生成器</param>
127- /// <param name="actionInvokers">action执行器</param>
128- /// <param name="fieldActionInterceptor">拦截器字段</param>
129- /// <param name="fieldActionInvokers">action执行器字段</param>
130- private static void BuildMethods ( TypeBuilder builder , IActionInvoker [ ] actionInvokers , FieldBuilder fieldActionInterceptor , FieldBuilder fieldActionInvokers )
131- {
132- const MethodAttributes implementAttribute = MethodAttributes . Public | MethodAttributes . Virtual | MethodAttributes . Final | MethodAttributes . NewSlot | MethodAttributes . HideBySig ;
119+ /// <summary>
120+ /// 生成代理类型的接口实现方法
121+ /// </summary>
122+ /// <param name="builder">类型生成器</param>
123+ /// <param name="actionInvokers">action执行器</param>
124+ /// <param name="fieldActionInterceptor">拦截器字段</param>
125+ /// <param name="fieldActionInvokers">action执行器字段</param>
126+ private static void BuildMethods ( TypeBuilder builder , IActionInvoker [ ] actionInvokers , FieldBuilder fieldActionInterceptor , FieldBuilder fieldActionInvokers )
127+ {
128+ const MethodAttributes implementAttribute = MethodAttributes . Public | MethodAttributes . Virtual | MethodAttributes . Final | MethodAttributes . NewSlot | MethodAttributes . HideBySig ;
133129
134- for ( var i = 0 ; i < actionInvokers . Length ; i ++ )
130+ for ( var i = 0 ; i < actionInvokers . Length ; i ++ )
131+ {
132+ var actionMethod = actionInvokers [ i ] . ApiAction . Member ;
133+ var actionParameters = actionMethod . GetParameters ( ) ;
134+ var parameterTypes = actionParameters . Select ( p => p . ParameterType ) . ToArray ( ) ;
135+
136+ var iL = builder
137+ . DefineMethod ( actionMethod . Name , implementAttribute , CallingConventions . Standard , actionMethod . ReturnType , parameterTypes )
138+ . GetILGenerator ( ) ;
139+
140+ // this.actionInterceptor
141+ iL . Emit ( OpCodes . Ldarg_0 ) ;
142+ iL . Emit ( OpCodes . Ldfld , fieldActionInterceptor ) ;
143+
144+ // this.actionInvokers[i]
145+ iL . Emit ( OpCodes . Ldarg_0 ) ;
146+ iL . Emit ( OpCodes . Ldfld , fieldActionInvokers ) ;
147+ iL . Emit ( OpCodes . Ldc_I4 , i ) ;
148+ iL . Emit ( OpCodes . Ldelem_Ref ) ;
149+
150+ // var arguments = new object[parameters.Length]
151+ var arguments = iL . DeclareLocal ( typeof ( object [ ] ) ) ;
152+ iL . Emit ( OpCodes . Ldc_I4 , actionParameters . Length ) ;
153+ iL . Emit ( OpCodes . Newarr , typeof ( object ) ) ;
154+ iL . Emit ( OpCodes . Stloc , arguments ) ;
155+
156+ for ( var j = 0 ; j < actionParameters . Length ; j ++ )
135157 {
136- var actionMethod = actionInvokers [ i ] . ApiAction . Member ;
137- var actionParameters = actionMethod . GetParameters ( ) ;
138- var parameterTypes = actionParameters . Select ( p => p . ParameterType ) . ToArray ( ) ;
139-
140- var iL = builder
141- . DefineMethod ( actionMethod . Name , implementAttribute , CallingConventions . Standard , actionMethod . ReturnType , parameterTypes )
142- . GetILGenerator ( ) ;
143-
144- // this.actionInterceptor
145- iL . Emit ( OpCodes . Ldarg_0 ) ;
146- iL . Emit ( OpCodes . Ldfld , fieldActionInterceptor ) ;
147-
148- // this.actionInvokers[i]
149- iL . Emit ( OpCodes . Ldarg_0 ) ;
150- iL . Emit ( OpCodes . Ldfld , fieldActionInvokers ) ;
151- iL . Emit ( OpCodes . Ldc_I4 , i ) ;
152- iL . Emit ( OpCodes . Ldelem_Ref ) ;
153-
154- // var arguments = new object[parameters.Length]
155- var arguments = iL . DeclareLocal ( typeof ( object [ ] ) ) ;
156- iL . Emit ( OpCodes . Ldc_I4 , actionParameters . Length ) ;
157- iL . Emit ( OpCodes . Newarr , typeof ( object ) ) ;
158- iL . Emit ( OpCodes . Stloc , arguments ) ;
159-
160- for ( var j = 0 ; j < actionParameters . Length ; j ++ )
161- {
162- iL . Emit ( OpCodes . Ldloc , arguments ) ;
163- iL . Emit ( OpCodes . Ldc_I4 , j ) ;
164- iL . Emit ( OpCodes . Ldarg , j + 1 ) ;
165-
166- var parameterType = parameterTypes [ j ] ;
167- if ( parameterType . IsValueType || parameterType . IsGenericParameter )
168- {
169- iL . Emit ( OpCodes . Box , parameterType ) ;
170- }
171- iL . Emit ( OpCodes . Stelem_Ref ) ;
172- }
173-
174- // 加载arguments参数
175158 iL . Emit ( OpCodes . Ldloc , arguments ) ;
159+ iL . Emit ( OpCodes . Ldc_I4 , j ) ;
160+ iL . Emit ( OpCodes . Ldarg , j + 1 ) ;
176161
177- // Intercep(actionInvoker, arguments)
178- iL . Emit ( OpCodes . Callvirt , interceptMethod ) ;
179-
180- if ( actionMethod . ReturnType == typeof ( void ) )
162+ var parameterType = parameterTypes [ j ] ;
163+ if ( parameterType . IsValueType || parameterType . IsGenericParameter )
181164 {
182- iL . Emit ( OpCodes . Pop ) ;
165+ iL . Emit ( OpCodes . Box , parameterType ) ;
183166 }
167+ iL . Emit ( OpCodes . Stelem_Ref ) ;
168+ }
169+
170+ // 加载arguments参数
171+ iL . Emit ( OpCodes . Ldloc , arguments ) ;
172+
173+ // Intercep(actionInvoker, arguments)
174+ iL . Emit ( OpCodes . Callvirt , interceptMethod ) ;
184175
185- iL . Emit ( OpCodes . Castclass , actionMethod . ReturnType ) ;
186- iL . Emit ( OpCodes . Ret ) ;
176+ if ( actionMethod . ReturnType == typeof ( void ) )
177+ {
178+ iL . Emit ( OpCodes . Pop ) ;
187179 }
180+
181+ iL . Emit ( OpCodes . Castclass , actionMethod . ReturnType ) ;
182+ iL . Emit ( OpCodes . Ret ) ;
188183 }
189184 }
190185 }
191- }
186+ }
0 commit comments