Only interfaces can be proxied using composition, so the target must implement one or more interfaces.
public void ClearWithDynamicProxies() { CompositionProxyTypeBuilder typeBuilder = new CompositionProxyTypeBuilder(); typeBuilder.TargetType = typeof(TestObject); Type proxyType = typeBuilder.BuildProxyType(); DefaultListableObjectFactory of = new DefaultListableObjectFactory(); RootObjectDefinition od1 = new RootObjectDefinition(proxyType, false); od1.PropertyValues.Add("Name", "Bruno"); of.RegisterObjectDefinition("testObject", od1); GenericApplicationContext ctx1 = new GenericApplicationContext(of); ContextRegistry.RegisterContext(ctx1); ITestObject to1 = ContextRegistry.GetContext().GetObject("testObject") as ITestObject; Assert.IsNotNull(to1); Assert.AreEqual("Bruno", to1.Name); DefaultListableObjectFactory of2 = new DefaultListableObjectFactory(); RootObjectDefinition od2 = new RootObjectDefinition(proxyType, false); od2.PropertyValues.Add("Name", "Baia"); of2.RegisterObjectDefinition("testObject", od2); GenericApplicationContext ctx2 = new GenericApplicationContext(of2); ContextRegistry.Clear(); ITestObject to2 = ctx2.GetObject("testObject") as ITestObject; Assert.IsNotNull(to2); Assert.AreEqual("Baia", to2.Name); }
public void CanBuildProxyForClassWithProtectedConstructor() { CompositionProxyTypeBuilder typeBuilder = new CompositionProxyTypeBuilder(); typeBuilder.TargetType = typeof(ClassWithProtectedCtor); typeBuilder.BuildProxyType(); }