1- using Castle . DynamicProxy ;
2- using JKang . IpcServiceFramework . IO ;
1+ using JKang . IpcServiceFramework . IO ;
32using System ;
43using System . IO ;
54using System . Linq ;
65using System . Linq . Expressions ;
6+ using System . Reflection ;
77using System . Threading ;
88using System . Threading . Tasks ;
99
@@ -12,7 +12,6 @@ namespace JKang.IpcServiceFramework.Client
1212 public abstract class IpcClient < TInterface > : IIpcClient < TInterface >
1313 where TInterface : class
1414 {
15- private static readonly ProxyGenerator _proxyGenerator = new ProxyGenerator ( ) ;
1615 private readonly IpcClientOptions _options ;
1716
1817 protected IpcClient (
@@ -31,7 +30,7 @@ protected IpcClient(
3130 public async Task InvokeAsync ( Expression < Action < TInterface > > exp ,
3231 CancellationToken cancellationToken = default )
3332 {
34- IpcRequest request = GetRequest ( exp , new MyInterceptor ( ) ) ;
33+ IpcRequest request = GetRequest ( exp , DispatchProxy . Create < TInterface , IpcProxy > ( ) ) ;
3534 IpcResponse response = await GetResponseAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
3635
3736 if ( ! response . Succeed ( ) )
@@ -46,7 +45,7 @@ public async Task InvokeAsync(Expression<Action<TInterface>> exp,
4645 public async Task < TResult > InvokeAsync < TResult > ( Expression < Func < TInterface , TResult > > exp ,
4746 CancellationToken cancellationToken = default )
4847 {
49- IpcRequest request = GetRequest ( exp , new MyInterceptor < TResult > ( ) ) ;
48+ IpcRequest request = GetRequest ( exp , DispatchProxy . Create < TInterface , IpcProxy < TResult > > ( ) ) ;
5049 IpcResponse response = await GetResponseAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
5150
5251 if ( ! response . Succeed ( ) )
@@ -68,7 +67,7 @@ public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, TRes
6867 public async Task InvokeAsync ( Expression < Func < TInterface , Task > > exp ,
6968 CancellationToken cancellationToken = default )
7069 {
71- IpcRequest request = GetRequest ( exp , new MyInterceptor < Task > ( ) ) ;
70+ IpcRequest request = GetRequest ( exp , DispatchProxy . Create < TInterface , IpcProxy < Task > > ( ) ) ;
7271 IpcResponse response = await GetResponseAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
7372
7473 if ( ! response . Succeed ( ) )
@@ -83,7 +82,7 @@ public async Task InvokeAsync(Expression<Func<TInterface, Task>> exp,
8382 public async Task < TResult > InvokeAsync < TResult > ( Expression < Func < TInterface , Task < TResult > > > exp ,
8483 CancellationToken cancellationToken = default )
8584 {
86- IpcRequest request = GetRequest ( exp , new MyInterceptor < Task < TResult > > ( ) ) ;
85+ IpcRequest request = GetRequest ( exp , DispatchProxy . Create < TInterface , IpcProxy < Task < TResult > > > ( ) ) ;
8786 IpcResponse response = await GetResponseAsync ( request , cancellationToken ) . ConfigureAwait ( false ) ;
8887
8988 if ( ! response . Succeed ( ) )
@@ -101,7 +100,7 @@ public async Task<TResult> InvokeAsync<TResult>(Expression<Func<TInterface, Task
101100 }
102101 }
103102
104- private static IpcRequest GetRequest ( Expression exp , MyInterceptor interceptor )
103+ private static IpcRequest GetRequest ( Expression exp , TInterface proxy )
105104 {
106105 if ( ! ( exp is LambdaExpression lambdaExp ) )
107106 {
@@ -113,21 +112,20 @@ private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
113112 throw new ArgumentException ( "Only support calling method, ex: x => x.GetData(a, b)" ) ;
114113 }
115114
116- TInterface proxy = _proxyGenerator . CreateInterfaceProxyWithoutTarget < TInterface > ( interceptor ) ;
117115 Delegate @delegate = lambdaExp . Compile ( ) ;
118116 @delegate . DynamicInvoke ( proxy ) ;
119117
120118 return new IpcRequest
121119 {
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 ,
124122
125- ParameterTypes = interceptor . LastInvocation . Method . GetParameters ( )
123+ ParameterTypes = ( proxy as IpcProxy ) . LastInvocation . Method . GetParameters ( )
126124 . Select ( p => p . ParameterType )
127125 . ToArray ( ) ,
128126
129127
130- GenericArguments = interceptor . LastInvocation . GenericArguments ,
128+ GenericArguments = ( proxy as IpcProxy ) . LastInvocation . Method . GetGenericArguments ( ) ,
131129 } ;
132130 }
133131
@@ -148,22 +146,35 @@ private async Task<IpcResponse> GetResponseAsync(IpcRequest request, Cancellatio
148146 }
149147 }
150148
151- private class MyInterceptor : IInterceptor
149+ public class IpcProxy : DispatchProxy
152150 {
153- public IInvocation LastInvocation { get ; private set ; }
151+ public Invocation LastInvocation { get ; protected set ; }
154152
155- public virtual void Intercept ( IInvocation invocation )
153+ protected override object Invoke ( MethodInfo targetMethod , object [ ] args )
156154 {
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 ; }
158169 }
159170 }
160171
161- private class MyInterceptor < TResult > : MyInterceptor
172+ public class IpcProxy < TResult > : IpcProxy
162173 {
163- public override void Intercept ( IInvocation invocation )
174+ protected override object Invoke ( MethodInfo targetMethod , object [ ] args )
164175 {
165- base . Intercept ( invocation ) ;
166- invocation . ReturnValue = default ( TResult ) ;
176+ LastInvocation = new Invocation ( targetMethod , args ) ;
177+ return default ( TResult ) ;
167178 }
168179 }
169180 }
0 commit comments