private static void Main(string[] args) { Form1 form1 = new Form1(); MyInterceptor myInterceptor = new MyInterceptor(form1); form1.ShowDialog(); }
private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor) { if (!(exp is LambdaExpression lambdaExp)) { throw new ArgumentException("Only support lambda expression, ex: x => x.GetData(a, b)"); } if (!(lambdaExp.Body is MethodCallExpression methodCallExp)) { throw new ArgumentException("Only support calling method, ex: x => x.GetData(a, b)"); } TInterface proxy = _proxyGenerator.CreateInterfaceProxyWithoutTarget <TInterface>(interceptor); Delegate @delegate = lambdaExp.Compile(); @delegate.DynamicInvoke(proxy); return(new IpcRequest { MethodName = interceptor.LastInvocation.Method.Name, Parameters = interceptor.LastInvocation.Arguments, ParameterTypes = interceptor.LastInvocation.Method.GetParameters() .Select(p => p.ParameterType) .ToArray(), GenericArguments = interceptor.LastInvocation.GenericArguments, }); }
public static void Show() { User user = new User() { Name = "Eleven", Password = "******" }; ProxyGenerator generator = new ProxyGenerator(); MyInterceptor interceptor = new MyInterceptor(); UserProcessor userprocessor = generator.CreateClassProxy <UserProcessor>(interceptor); userprocessor.RegUser(user); }
public async Task CreatePorxyTest() { var config = new HttpApiConfig(); var interceptor = new MyInterceptor(config); var myApi = HttpApiClientProxy.CreateInstance(typeof(IMyApi), interceptor) as IMyApi; var result = await myApi.M1(0, 1); Assert.Equal(result.Method, typeof(IMyApi).GetMethod("M1")); Assert.True(result.Parameters.Length == 2); Assert.True((int)result.Parameters.First() == 0); Assert.True((int)result.Parameters.Last() == 1); }
public static void Show() { User user = new User() { Name = "Richard", Password = "******" }; Castle.DynamicProxy.ProxyGenerator generator = new Castle.DynamicProxy.ProxyGenerator(); MyInterceptor interceptor = new MyInterceptor(); UserProcessor userprocessor = generator.CreateClassProxy <UserProcessor>(interceptor); userprocessor.RegUser(user); }
public void InterceptCallToPropertyInNonInterceptedMethod() { bool wasIntercepted = false; MyInterceptor interceptor = new MyInterceptor(); interceptor.Intercepted += (s, e) => { wasIntercepted = true; }; Person p = new Person { Name = "John" }; p.SetAgeTo(50); p = Decorate(p, interceptor); p.SetAgeTo(100); var originalObject = p.GetType().GetField("__target").GetValue(p); var ageInOriginalObject = originalObject.GetType().GetProperty("Age", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(originalObject, null); Assert.That(ageInOriginalObject, Is.EqualTo(100)); // The value is still 50 Assert.That(wasIntercepted, Is.True); // The call was not intercepted }
public void EstablishContext() { ExecutionSequence = new StringBuilder(); Interceptor = new MyInterceptor(ExecutionSequence); }