/// <summary> /// Initializes a new instance of the <see cref="PropertyBasedInterceptorDecoration"/> class. /// </summary> /// <param name="property">The property.</param> /// <param name="getMethodBasedInterceptor">The get method based interceptor.</param> /// <param name="setMethodBasedInterceptor">The set method based interceptor.</param> /// <exception cref="ArgumentNullException"> Specified <paramref name="property"/> is null.</exception> /// <exception cref="ArgumentNullException"> Specified <paramref name="getMethodBasedInterceptor"/> and <paramref name="setMethodBasedInterceptor"/> are both null.</exception> public PropertyBasedInterceptorDecoration( PropertyInfo property, InterceptorDelegate getMethodBasedInterceptor, InterceptorDelegate setMethodBasedInterceptor) { this.Property = Guard.ArgumentNotNull(property, nameof(property)); if (getMethodBasedInterceptor == null && setMethodBasedInterceptor == null) { throw new ArgumentException(Resources.ExceptionGetAndSetMethodBasedInterceptorCannotBeNull); } if (getMethodBasedInterceptor != null) { var getMethod = property.GetMethod; if (null != getMethod) { this.GetMethodBasedInterceptor = new MethodBasedInterceptorDecoration(getMethod, getMethodBasedInterceptor); } } if (setMethodBasedInterceptor != null) { var setMethod = property.SetMethod; if (null != setMethod) { this.SetMethodBasedInterceptor = new MethodBasedInterceptorDecoration(setMethod, setMethodBasedInterceptor); } } }
private T CreateProxy <T>(T target, InterceptorDelegate interceptor, MethodInfo method) { var methodBasedInterceptor = new MethodBasedInterceptorDecoration(method, interceptor); var decoration = new InterceptorDecoration(new MethodBasedInterceptorDecoration[] { methodBasedInterceptor }, null); var generator = DynamicProxyClassGenerator.CreateInterfaceGenerator(typeof(T), decoration); var proxyType = generator.GenerateProxyType(); return((T)Activator.CreateInstance(proxyType, target, decoration)); }
private T CreateProxy <T>(InterceptorDelegate interceptor, MethodInfo method) { var methodBasedInterceptor = new MethodBasedInterceptorDecoration(method, interceptor); var decoration = new InterceptorDecoration(new MethodBasedInterceptorDecoration[] { methodBasedInterceptor }, null); var generator = DynamicProxyClassGenerator.CreateVirtualMethodGenerator(typeof(T), decoration); var proxyType = generator.GenerateProxyType(); var proxy = (T)Activator.CreateInstance(proxyType); ((IInterceptorsInitializer)proxy).SetInterceptors(decoration); return(proxy); }