public void BuildRuntimeType_MethodInterceptors_InterceptInterfaceMethod()
        {
            // Arrange
            Mock <IMockableRuntimeTypePropertyManager> managerMock = new Mock <IMockableRuntimeTypePropertyManager>(MockBehavior.Strict);
            var        manager   = managerMock.Object;
            MethodInfo getMethod = manager.GetType().GetMethod("GetValue");
            MethodInfo setMethod = manager.GetType().GetMethod("SetValue");

            bool   interceptorCalled = false;
            Action interceptor       = () => interceptorCalled = true;

            MethodInterceptions interceptions = new MethodInterceptions
            {
                Interceptions = new MethodInterception[]
                {
                    new DelegateMethodInterception
                    {
                        InterceptedMethod = typeof(IInterfaceWithMethod).GetMethod(nameof(IInterfaceWithMethod.MethodToIntercept)),
                        Delegate          = interceptor
                    }
                }
            };

            // Act
            Stopwatch            sw          = Stopwatch.StartNew();
            Type                 runtimeType = RuntimeProxyBuilder.BuildRuntimeType(typeof(IInterfaceWithMethod), getMethod, setMethod, interceptions);
            IInterfaceWithMethod impl        = (IInterfaceWithMethod)Activator.CreateInstance(runtimeType, new object[] { manager, interceptions });

            impl.MethodToIntercept();
            sw.Stop();
            Trace.WriteLine(sw.ElapsedMilliseconds);

            // Assert
            Assert.IsTrue(interceptorCalled);
        }
        public void BuildRuntimeType_MethodInterceptors_Function_InterceptClassMethod_ReturnParentValue()
        {
            // Arrange
            Mock <IMockableRuntimeTypePropertyManager> managerMock = new Mock <IMockableRuntimeTypePropertyManager>(MockBehavior.Strict);
            var        manager   = managerMock.Object;
            MethodInfo getMethod = manager.GetType().GetMethod("GetValue");
            MethodInfo setMethod = manager.GetType().GetMethod("SetValue");

            string expectedValue = "base";
            Func <Func <string>, string> interceptor = baseMethod => baseMethod();

            MethodInterceptions interceptions = new MethodInterceptions
            {
                Interceptions = new MethodInterception[]
                {
                    new DelegateMethodInterception
                    {
                        InterceptedMethod = typeof(ClassWithVirtualMethod).GetMethod(nameof(ClassWithVirtualMethod.FunctionToIntercept)),
                        Delegate          = interceptor
                    }
                }
            };

            // Act
            Stopwatch sw                = Stopwatch.StartNew();
            Type      runtimeType       = RuntimeProxyBuilder.BuildRuntimeType(typeof(ClassWithVirtualMethod), getMethod, setMethod, interceptions);
            ClassWithVirtualMethod impl = (ClassWithVirtualMethod)Activator.CreateInstance(runtimeType, new object[] { manager, interceptions });
            string returnedValue        = impl.FunctionToIntercept();

            sw.Stop();
            Trace.WriteLine(sw.ElapsedMilliseconds);

            // Assert
            Assert.AreEqual(expectedValue, returnedValue);
        }
        public void BuildRuntimeType_MethodInterceptors_CallBase()
        {
            // Arrange
            Mock <IMockableRuntimeTypePropertyManager> managerMock = new Mock <IMockableRuntimeTypePropertyManager>(MockBehavior.Strict);
            var        manager   = managerMock.Object;
            MethodInfo getMethod = manager.GetType().GetMethod("GetValue");
            MethodInfo setMethod = manager.GetType().GetMethod("SetValue");

            Action <Action>     interceptor   = baseAction => baseAction();
            MethodInterceptions interceptions = new MethodInterceptions
            {
                Interceptions = new MethodInterception[]
                {
                    new DelegateMethodInterception
                    {
                        InterceptedMethod = typeof(ClassWithVirtualMethod).GetMethod(nameof(ClassWithVirtualMethod.ActionToIntercept)),
                        Delegate          = interceptor
                    }
                }
            };

            // Act
            Stopwatch sw                = Stopwatch.StartNew();
            Type      runtimeType       = RuntimeProxyBuilder.BuildRuntimeType(typeof(ClassWithVirtualMethod), getMethod, setMethod, interceptions);
            ClassWithVirtualMethod impl = (ClassWithVirtualMethod)Activator.CreateInstance(runtimeType, new object[] { manager, interceptions });

            impl.ActionToIntercept();
            sw.Stop();
            Trace.WriteLine(sw.ElapsedMilliseconds);

            // Assert
            Assert.IsTrue(impl.InterceptedMethodCalled);
        }
        public RuntimeProxyManager(
            IObservableProperty observableProperty,
            MethodInterceptions methodInterceptions,
            bool readOnly)
        {
            ObservableProperty       = observableProperty;
            this.methodInterceptions = methodInterceptions;
            IsReadOnly        = readOnly;
            observableFactory = observableProperty.CreateFactory();

            Initialize();
        }
        private void InitializeStoreReactables()
        {
            RegisterStoreAutoruns();

            List <MethodInterception> methodInterceptions = new List <MethodInterception>();

            methodInterceptions.AddRange(GetStoreComputedValueInterceptions());
            methodInterceptions.AddRange(GetStoreActionInterceptions());
            StoreReactables = new MethodInterceptions
            {
                Interceptions = methodInterceptions.ToArray()
            };
        }
        private static Type GetRuntimeType(MethodInterceptions interceptions)
        {
            if (interceptions == null)
            {
                return(runtimeTypeWithoutInterceptions.Value);
            }

            Type type;

            lock (typeof(TInterface))
            {
                if (!typeCache.TryGetValue(interceptions, out type))
                {
                    type = RuntimeProxyBuilder.BuildRuntimeType(typeof(TInterface), getMethod, setMethod, interceptions);
                    typeCache.Add(interceptions, type);
                }
            }

            return(type);
        }
Example #7
0
 public IPropertyProxy Create(IObservableProperty observableProperty, MethodInterceptions interceptions, bool readOnly = false)
 {
     return(RuntimeProxyManagerHelper.CreateRuntimeManager(observableProperty, interceptions, readOnly));
 }
 public RuntimeProxyManager(
     IObservableProperty observableProperty,
     MethodInterceptions methodInterceptions) : this(observableProperty, methodInterceptions, false)
 {
 }
 public IPropertyProxy Create(IObservableProperty observableProperty, MethodInterceptions interceptions, bool readOnly = false)
 {
     throw new NotImplementedException();
 }
        private static Type BuildRuntimeTypeInternal(Type baseType, MethodInfo getMethod, MethodInfo setMethod, MethodInterceptions methodInterceptions = null)
        {
            TypeBuilder  typeBuilder           = CreateTypeBuilder(baseType);
            FieldBuilder managerField          = AddManagerProperty(typeBuilder);
            FieldBuilder capturedContextsField = AddCapturedContextsField(typeBuilder);

            bool isClass    = baseType.IsClass;
            var  properties = GetPublicProperties(baseType).Where(x => x.GetMethod.IsAbstract || (isClass && x.GetMethod.IsVirtual && x.SetMethod != null));

            foreach (var propertyInfo in properties)
            {
                AddProperty(typeBuilder, propertyInfo, managerField, getMethod, setMethod);
            }

            AddConstructors(baseType, typeBuilder, managerField, capturedContextsField, properties);

            if (methodInterceptions != null)
            {
                DelegateWrapperTypeContainer container = new DelegateWrapperBuilder(typeBuilder, baseType).AddDelegateWrapperType();

                var methodBuilder = new InterceptedMethodBuilder(container, typeBuilder, capturedContextsField);
                foreach (var methodInterception in methodInterceptions)
                {
                    methodBuilder.DefineInterceptedMethod(methodInterception);
                }
                container.TypeBuilder.CreateType();
            }

            return(typeBuilder.CreateType());
        }
 public static Type BuildRuntimeType(Type baseType, MethodInfo getMethod, MethodInfo setMethod, MethodInterceptions methodInterceptions = null)
 {
     return(BuildRuntimeTypeInternal(baseType, getMethod, setMethod, methodInterceptions));
 }