1
- using Castle . DynamicProxy ;
2
- using JKang . IpcServiceFramework . IO ;
1
+ using JKang . IpcServiceFramework . IO ;
3
2
using System ;
4
3
using System . IO ;
5
4
using System . Linq ;
6
5
using System . Linq . Expressions ;
6
+ using System . Reflection ;
7
7
using System . Threading ;
8
8
using System . Threading . Tasks ;
9
9
@@ -12,7 +12,6 @@ namespace JKang.IpcServiceFramework.Client
12
12
public abstract class IpcClient < TInterface > : IIpcClient < TInterface >
13
13
where TInterface : class
14
14
{
15
- private static readonly ProxyGenerator _proxyGenerator = new ProxyGenerator ( ) ;
16
15
private readonly IpcClientOptions _options ;
17
16
18
17
protected IpcClient (
@@ -31,7 +30,7 @@ protected IpcClient(
31
30
public async Task InvokeAsync ( Expression < Action < TInterface > > exp ,
32
31
CancellationToken cancellationToken = default )
33
32
{
34
- IpcRequest request = GetRequest ( exp , new MyInterceptor ( ) ) ;
33
+ IpcRequest request = GetRequest ( exp , DispatchProxy . Create < TInterface , IpcProxy > ( ) ) ;
35
34
IpcResponse response = await GetResponseAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
36
35
37
36
if ( ! response . Succeed ( ) )
@@ -46,7 +45,7 @@ public async Task InvokeAsync(Expression<Action<TInterface>> exp,
46
45
public async Task < TResult > InvokeAsync < TResult > ( Expression < Func < TInterface , TResult > > exp ,
47
46
CancellationToken cancellationToken = default )
48
47
{
49
- IpcRequest request = GetRequest ( exp , new MyInterceptor < TResult > ( ) ) ;
48
+ IpcRequest request = GetRequest ( exp , DispatchProxy . Create < TInterface , IpcProxy < TResult > > ( ) ) ;
50
49
IpcResponse response = await GetResponseAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
51
50
52
51
if ( ! response . Succeed ( ) )
@@ -68,7 +67,7 @@ public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, TRes
68
67
public async Task InvokeAsync ( Expression < Func < TInterface , Task > > exp ,
69
68
CancellationToken cancellationToken = default )
70
69
{
71
- IpcRequest request = GetRequest ( exp , new MyInterceptor < Task > ( ) ) ;
70
+ IpcRequest request = GetRequest ( exp , DispatchProxy . Create < TInterface , IpcProxy < Task > > ( ) ) ;
72
71
IpcResponse response = await GetResponseAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
73
72
74
73
if ( ! response . Succeed ( ) )
@@ -83,7 +82,7 @@ public async Task InvokeAsync(Expression<Func<TInterface, Task>> exp,
83
82
public async Task < TResult > InvokeAsync < TResult > ( Expression < Func < TInterface , Task < TResult > > > exp ,
84
83
CancellationToken cancellationToken = default )
85
84
{
86
- IpcRequest request = GetRequest ( exp , new MyInterceptor < Task < TResult > > ( ) ) ;
85
+ IpcRequest request = GetRequest ( exp , DispatchProxy . Create < TInterface , IpcProxy < Task < TResult > > > ( ) ) ;
87
86
IpcResponse response = await GetResponseAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
88
87
89
88
if ( ! response . Succeed ( ) )
@@ -101,7 +100,7 @@ public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, Task
101
100
}
102
101
}
103
102
104
- private static IpcRequest GetRequest ( Expression exp , MyInterceptor interceptor )
103
+ private static IpcRequest GetRequest ( Expression exp , TInterface proxy )
105
104
{
106
105
if ( ! ( exp is LambdaExpression lambdaExp ) )
107
106
{
@@ -113,21 +112,20 @@ private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
113
112
throw new ArgumentException ( "Only support calling method, ex: x => x.GetData(a, b)" ) ;
114
113
}
115
114
116
- TInterface proxy = _proxyGenerator . CreateInterfaceProxyWithoutTarget < TInterface > ( interceptor ) ;
117
115
Delegate @delegate = lambdaExp . Compile ( ) ;
118
116
@delegate . DynamicInvoke ( proxy ) ;
119
117
120
118
return new IpcRequest
121
119
{
122
- MethodName = interceptor . LastInvocation . Method . Name ,
123
- Parameters = interceptor . LastInvocation . Arguments ,
120
+ MethodName = ( proxy as IpcProxy ) . LastInvocation . Method . Name ,
121
+ Parameters = ( proxy as IpcProxy ) . LastInvocation . Arguments ,
124
122
125
- ParameterTypes = interceptor . LastInvocation . Method . GetParameters ( )
123
+ ParameterTypes = ( proxy as IpcProxy ) . LastInvocation . Method . GetParameters ( )
126
124
. Select ( p => p . ParameterType )
127
125
. ToArray ( ) ,
128
126
129
127
130
- GenericArguments = interceptor . LastInvocation . GenericArguments ,
128
+ GenericArguments = ( proxy as IpcProxy ) . LastInvocation . Method . GetGenericArguments ( ) ,
131
129
} ;
132
130
}
133
131
@@ -148,22 +146,35 @@ private async Task<IpcResponse> GetResponseAsync(IpcRequest request, Cancellatio
148
146
}
149
147
}
150
148
151
- private class MyInterceptor : IInterceptor
149
+ public class IpcProxy : DispatchProxy
152
150
{
153
- public IInvocation LastInvocation { get ; private set ; }
151
+ public Invocation LastInvocation { get ; protected set ; }
154
152
155
- public virtual void Intercept ( IInvocation invocation )
153
+ protected override object Invoke ( MethodInfo targetMethod , object [ ] args )
156
154
{
157
- LastInvocation = invocation ;
155
+ LastInvocation = new Invocation ( targetMethod , args ) ;
156
+ return null ;
157
+ }
158
+
159
+ public class Invocation
160
+ {
161
+ public Invocation ( MethodInfo method , object [ ] args )
162
+ {
163
+ Method = method ;
164
+ Arguments = args ;
165
+ }
166
+
167
+ public MethodInfo Method { get ; }
168
+ public object [ ] Arguments { get ; }
158
169
}
159
170
}
160
171
161
- private class MyInterceptor < TResult > : MyInterceptor
172
+ public class IpcProxy < TResult > : IpcProxy
162
173
{
163
- public override void Intercept ( IInvocation invocation )
174
+ protected override object Invoke ( MethodInfo targetMethod , object [ ] args )
164
175
{
165
- base . Intercept ( invocation ) ;
166
- invocation . ReturnValue = default ( TResult ) ;
176
+ LastInvocation = new Invocation ( targetMethod , args ) ;
177
+ return default ( TResult ) ;
167
178
}
168
179
}
169
180
}
0 commit comments