Beispiel #1
0
        public void Create_WithDefaultHandler_GetProxyInstance()
        {
            var list = new Collection <object>()
            {
                new object()
            };
            var handler = DispatchProxyServices.DefaultHandler;
            var pList   = DispatchProxy <IList <object> > .Create(list, handler);

            Assert.IsNotNull(pList);
            Assert.IsInstanceOfType(pList, typeof(DispatchProxy <IList <object> >));
            Assert.AreEqual(1, pList.Count);
        }
Beispiel #2
0
        /// <summary>
        /// 使用指定的代理对象和代理委托创建 <see cref="DispatchProxy{TInterface}"/> 类的实例。
        /// </summary>
        /// <param name="instance">要代理的 <typeparamref name="TInterface"/> 类型的对象。</param>
        /// <param name="handler">方法调用时所用的 <see cref="InvocationHandler"/> 代理委托。</param>
        /// <returns>实现 <typeparamref name="TInterface"/> 接口的代理类型的对象。</returns>
        /// <exception cref="ArgumentNullException"><paramref name="instance"/>
        /// 或 <paramref name="handler"/> 为 <see langword="null"/>。</exception>
        public static TInterface Create(TInterface instance, InvocationHandler handler)
        {
            if (instance is null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (handler is null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            var proxy = DispatchProxy.Create <TInterface, DispatchProxy <TInterface> >();

            ((DispatchProxy <TInterface>)(object) proxy).Instance = instance;
            ((DispatchProxy <TInterface>)(object) proxy).Handler  = handler;
            return(proxy);
        }
Beispiel #3
0
        public void Create_WithCallCountHandler_GetExpectedCount()
        {
            var list = new Collection <object?>()
            {
                new object()
            };
            var callCounts = new Dictionary <MethodInfo, int>();
            var handler    = (InvocationHandler)((instance, method, arguments) =>
            {
                callCounts[method] = callCounts.GetOrAdd(method, 0) + 1;
                return(method.GetDynamicDelegate().Invoke(instance, arguments));
            });
            var pList = DispatchProxy <IList <object?> > .Create(list, handler);

            Assert.AreEqual(0, callCounts.Count);
            pList.Add(new object());
            Assert.AreEqual(1, callCounts.Count);
            Assert.AreEqual(2, pList.Count);
            Assert.AreEqual(2, callCounts.Count);
            pList[0] = null;
            Assert.AreEqual(3, callCounts.Count);
            Assert.IsNull(pList[0]);
            Assert.AreEqual(4, callCounts.Count);
        }
Beispiel #4
0
 /// <summary>
 /// 使用指定的代理对象和代理委托创建 <see cref="DispatchProxy{TInterface}"/> 类的实例。
 /// </summary>
 /// <typeparam name="TInterface">代理应实现的接口类型。</typeparam>
 /// <param name="instance">要代理的 <typeparamref name="TInterface"/> 类型的对象。</param>
 /// <param name="handler">方法调用时所用的 <see cref="InvocationHandler"/> 代理委托。</param>
 /// <returns>实现 <typeparamref name="TInterface"/> 接口的代理类型的对象。</returns>
 /// <exception cref="ArgumentNullException"><paramref name="instance"/>
 /// 或 <paramref name="handler"/> 为 <see langword="null"/>。</exception>
 public static TInterface CreateProxy <TInterface>(TInterface instance, InvocationHandler?handler = null)
     where TInterface : class
 {
     handler ??= DispatchProxyServices.DefaultHandler;
     return(DispatchProxy <TInterface> .Create(instance, handler));
 }