public void GeneratedTypeForAdditionalInterfaceWithMethodsHavingSignaturesMatchingMethodsInTheBaseClassIsVerifiable() { PermissionSet grantSet = new PermissionSet(PermissionState.None); grantSet.AddPermission( new SecurityPermission( SecurityPermissionFlag.Execution | SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy)); grantSet.AddPermission( new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess | ReflectionPermissionFlag.ReflectionEmit | ReflectionPermissionFlag.MemberAccess)); grantSet.AddPermission(new FileIOPermission(PermissionState.Unrestricted)); AppDomain sandbox = AppDomain.CreateDomain( "sandbox", AppDomain.CurrentDomain.Evidence, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory }, grantSet); sandbox.DoCallBack(() => { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(MainType), typeof(IDoSomething), typeof(IDoSomethingToo)); Type generatedType = generator.GenerateType(); }); }
public void CanInvokeMethodsFromDifferentTypesWithMatchingSignatures() { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(MainType), typeof(IDoSomething), typeof(IDoSomethingToo)); Type generatedType = generator.GenerateType(); object instance = Activator.CreateInstance(generatedType); List <MethodBase> invokedMethods = new List <MethodBase>(); ((IInterceptingProxy)instance) .AddInterceptionBehavior( new DelegateInterceptionBehavior((mi, n) => { invokedMethods.Add(mi.MethodBase); return(mi.CreateMethodReturn(1)); })); ((MainType)instance).DoSomething(); ((IDoSomething)instance).DoSomething(); ((IDoSomethingToo)instance).DoSomething(); Assert.AreSame(StaticReflection.GetMethodInfo <MainType>(i => i.DoSomething()), invokedMethods[0]); Assert.AreSame(StaticReflection.GetMethodInfo <IDoSomething>(i => i.DoSomething()), invokedMethods[1]); Assert.AreSame(StaticReflection.GetMethodInfo <IDoSomethingToo>(i => i.DoSomething()), invokedMethods[2]); }
public void GeneratedTypeForAdditionalInterfaceWithMethodsHavingSignaturesMatchingMethodsInTheBaseClassIsVerifiable() { PermissionSet grantSet = new PermissionSet(PermissionState.None); grantSet.AddPermission( new SecurityPermission( SecurityPermissionFlag.Execution | SecurityPermissionFlag.ControlEvidence | SecurityPermissionFlag.ControlPolicy)); grantSet.AddPermission( new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess | ReflectionPermissionFlag.MemberAccess)); grantSet.AddPermission(new FileIOPermission(PermissionState.Unrestricted)); AppDomain sandbox = AppDomain.CreateDomain( "sandbox", AppDomain.CurrentDomain.Evidence, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory }, grantSet); sandbox.DoCallBack(() => { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(MainType), typeof(IDoSomething), typeof(IDoSomethingToo)); Type generatedType = generator.GenerateType(); }); }
public void InterceptingClassOverridesBaseClassVirtualMethods() { Type baseType = typeof(ClassWithDefaultCtor); VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor(); InterceptingClassGenerator generator = new InterceptingClassGenerator(baseType); Type generatedType = generator.GenerateType(); MethodInfo methodOne = generatedType.GetMethod("MethodOne"); MethodInfo calculateAnswer = generatedType.GetMethod("CalculateAnswer"); Assert.AreSame(generatedType, methodOne.DeclaringType); Assert.AreSame(generatedType, calculateAnswer.DeclaringType); }
public void CanImplementAdditionalInterfaceWithMethodsHavingSignaturesMatchingMethodsInTheBaseClass() { // arrange InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(MainType), typeof(IDoSomething)); Type generatedType = generator.GenerateType(); // act object instance = Activator.CreateInstance(generatedType); // assert Assert.IsTrue(instance is MainType); Assert.IsTrue(instance is IDoSomething); }
public void CanImplementAdditionalInterfaces() { // arrange InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(MainType), typeof(IAdditionalInterface)); Type generatedType = generator.GenerateType(); // act object instance = Activator.CreateInstance(generatedType); // assert Assert.IsTrue(instance is MainType); Assert.IsTrue(instance is IAdditionalInterface); }
public void CanInterceptClassWithReservedTypeAttributes() { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(ClassWithPermissionAttribute)); Type generatedType = generator.GenerateType(); ClassWithPermissionAttribute instance = (ClassWithPermissionAttribute)Activator.CreateInstance(generatedType); int intercepts = 0; ((IInterceptingProxy)instance) .AddInterceptionBehavior( new DelegateInterceptionBehavior((mi, gn) => { intercepts++; return(gn()(mi, gn)); })); instance.Method(); Assert.AreEqual(1, intercepts); }
public void CanSuccessfullyInvokeAnAdditionalInterfaceMethodIfAnInterceptorDoesNotForwardTheCall() { // arrange InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(MainType), typeof(IAdditionalInterface)); Type generatedType = generator.GenerateType(); object instance = Activator.CreateInstance(generatedType); bool invoked = false; ((IInterceptingProxy)instance).AddInterceptionBehavior( new DelegateInterceptionBehavior( (input, getNext) => { invoked = true; return(input.CreateMethodReturn(100)); })); // act int result = ((IAdditionalInterface)instance).DoSomethingElse(); // assert Assert.IsTrue(invoked); Assert.AreEqual(100, result); }
public void CanInterceptClassWithReservedTypeAttributes() { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(HtmlInputText)); Type generatedType = generator.GenerateType(); HtmlInputText instance = (HtmlInputText)Activator.CreateInstance(generatedType); bool intercepted = false; ((IInterceptingProxy)instance) .AddInterceptionBehavior( new ActionInterceptionBehavior(() => intercepted = true)); var result = instance.HasControls(); Assert.IsTrue(intercepted); }
internal static T GetInterceptingInstance <T>(params object[] ctorValues) { Type typeToIntercept = typeof(T); if (typeToIntercept.IsGenericType) { typeToIntercept = typeToIntercept.GetGenericTypeDefinition(); } InterceptingClassGenerator generator = new InterceptingClassGenerator(typeToIntercept); Type generatedType = generator.GenerateType(); if (generatedType.IsGenericTypeDefinition) { generatedType = generatedType.MakeGenericType(typeof(T).GetGenericArguments()); } return((T)Activator.CreateInstance(generatedType, ctorValues)); }
/// <summary> /// Create a type to proxy for the given type <paramref name="t"/>. /// </summary> /// <param name="t">Type to proxy.</param> /// <param name="additionalInterfaces">Additional interfaces the proxy must implement.</param> /// <returns>New type that can be instantiated instead of the /// original type t, and supports interception.</returns> public Type CreateProxyType(Type t, params Type[] additionalInterfaces) { Guard.ArgumentNotNull(t, "t"); Guard.ArgumentNotNull(additionalInterfaces, "additionalInterfaces"); if (!CanIntercept(t)) { throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, Resources.InterceptionNotSupported, t.Name)); } Type interceptorType; Type typeToDerive = t; bool genericType = false; if (t.IsGenericType) { typeToDerive = t.GetGenericTypeDefinition(); genericType = true; } GeneratedTypeKey key = new GeneratedTypeKey(typeToDerive, additionalInterfaces); lock (DerivedClasses) { if (!DerivedClasses.TryGetValue(key, out interceptorType)) { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeToDerive, additionalInterfaces); interceptorType = generator.GenerateType(); DerivedClasses[key] = interceptorType; } } if (genericType) { interceptorType = interceptorType.MakeGenericType(t.GetGenericArguments()); } return(interceptorType); }
public void DoesNotReImplementAdditionalInterfaceAlreadyImplementedByInterceptedClass() { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(InterfaceImplementingMainType), typeof(IDeriveFromIDoSomething)); Type generatedType = generator.GenerateType(); object instance = Activator.CreateInstance(generatedType); List <MethodBase> interceptedMethods = new List <MethodBase>(); ((IInterceptingProxy)instance) .AddInterceptionBehavior( new DelegateInterceptionBehavior((mi, n) => { interceptedMethods.Add(mi.MethodBase); return(mi.CreateMethodReturn(1)); })); ((InterfaceImplementingMainType)instance).DoSomething(); ((InterfaceImplementingMainType)instance).DoSomething(""); ((IDoSomething)instance).DoSomething(); ((IDoSomething)instance).DoSomething(""); ((IDeriveFromIDoSomething)instance).DoSomething(); ((IDeriveFromIDoSomething)instance).DoSomething(""); ((IDeriveFromIDoSomething)instance).DoSomethingElse(); Assert.AreEqual(4, interceptedMethods.Count); // only the virtual implicit method implementation is invoked for IDoSomething Assert.AreSame( StaticReflection.GetMethodInfo <InterfaceImplementingMainType>(i => i.DoSomething(null)), interceptedMethods[0]); Assert.AreSame( StaticReflection.GetMethodInfo <InterfaceImplementingMainType>(i => i.DoSomething(null)), interceptedMethods[1]); Assert.AreSame( StaticReflection.GetMethodInfo <InterfaceImplementingMainType>(i => i.DoSomething(null)), interceptedMethods[2]); Assert.AreSame( StaticReflection.GetMethodInfo <IDeriveFromIDoSomething>(i => i.DoSomethingElse()), interceptedMethods[3]); }
public void DoesNotInterceptInternalVirtualProperties() { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(ClassWithInternalProperty)); Type generatedType = generator.GenerateType(); ClassWithInternalProperty instance = (ClassWithInternalProperty)Activator.CreateInstance(generatedType); int intercepts = 0; ((IInterceptingProxy)instance) .AddInterceptionBehavior( new DelegateInterceptionBehavior((mi, gn) => { intercepts++; return(gn()(mi, gn)); })); instance.SetProperty(10); int value = instance.GetProperty(); Assert.AreEqual(10, value); Assert.AreEqual(0, intercepts); }
public void InvokingMethodOnAdditionalInterfaceThrowsIfNotHandledByInterceptor() { // arrange InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(MainType), typeof(IAdditionalInterface)); Type generatedType = generator.GenerateType(); object instance = Activator.CreateInstance(generatedType); // act Exception exception = null; try { ((IAdditionalInterface)instance).DoSomethingElse(); Assert.Fail("should have thrown"); } catch (NotImplementedException e) { exception = e; } // assert Assert.IsNotNull(exception); }
public void CanImplementINotifyPropertyChanged() { InterceptingClassGenerator generator = new InterceptingClassGenerator(typeof(MainType), typeof(INotifyPropertyChanged)); Type generatedType = generator.GenerateType(); object instance = Activator.CreateInstance(generatedType); string changeProperty = null; PropertyChangedEventHandler handler = (sender, args) => changeProperty = args.PropertyName; ((IInterceptingProxy)instance).AddInterceptionBehavior(new NaiveINotifyPropertyChangedInterceptionBehavior()); ((INotifyPropertyChanged)instance).PropertyChanged += handler; ((MainType)instance).IntProperty = 100; Assert.AreEqual(100, ((MainType)instance).IntProperty); Assert.AreEqual("IntProperty", changeProperty); changeProperty = null; ((INotifyPropertyChanged)instance).PropertyChanged -= handler; ((MainType)instance).IntProperty = 200; Assert.AreEqual(200, ((MainType)instance).IntProperty); Assert.AreEqual(null, changeProperty); }