public void InterceptExtendedProperties() { Engine c = new Engine("InterceptExtendedProperties"); SignatureAspect aspect = new SignatureAspect("PropertyAdder", typeof(Foo), new Type[] { }, new IPointcut[] { }); aspect.Pointcuts.Add(new SignaturePointcut("get_MyIntProperty", new IncreaseReturnValueInterceptor())); TypeExtender extender = new TypeExtender(); ExtendedProperty property1 = new ExtendedProperty(); property1.Name = "MyIntProperty"; property1.FieldName = "_MyIntProperty"; property1.Type = typeof(int); extender.Members.Add(property1); aspect.TypeExtenders.Add(extender); c.Configuration.Aspects.Add(aspect); Foo proxy = (Foo)c.CreateProxy(typeof(Foo)); PropertyInfo property1Info = proxy.GetType().GetProperty("MyIntProperty"); Assert.IsNotNull(property1, "Property1 was not emitted"); property1Info.SetValue(proxy, 123, null); int resInt = (int)property1Info.GetValue(proxy, null); Assert.IsTrue(resInt == 124, "Property1 Was not intercepted"); }
private IList GetExtensionAspects() { IList extensionAspects = new ArrayList(); foreach (IClassMap classMap in this.Context.DomainMap.ClassMaps) { IList generatedPropertyMaps = classMap.GetGeneratedPropertyMaps(); if (generatedPropertyMaps.Count > 0) { Type targetType = AssemblyManager.GetBaseType( this.context.AssemblyManager.MustGetTypeFromClassMap(classMap)); TypeExtender extender = new TypeExtender(); SignatureAspect aspect = new SignatureAspect(classMap.Name + "GeneratedPropertiesExtender", targetType, new Type[] { }, new IPointcut[] { }); foreach (IPropertyMap generatedPropertyMap in generatedPropertyMaps) { ExtendedProperty property = new ExtendedProperty(); property.Name = generatedPropertyMap.Name; property.FieldName = generatedPropertyMap.GetFieldName(); if (generatedPropertyMap.IsCollection) property.Type = typeof(IList); else { if (generatedPropertyMap.ReferenceType != ReferenceType.None) { IClassMap refClassMap = generatedPropertyMap.MustGetReferencedClassMap(); property.Type = AssemblyManager.GetBaseType( this.context.AssemblyManager.MustGetTypeFromClassMap(refClassMap)); } else { property.Type = Type.GetType(generatedPropertyMap.DataType); } } extender.Members.Add(property); } aspect.TypeExtenders.Add(extender); extensionAspects.Add(aspect); } } return extensionAspects; }
public void ExtendingProperties() { Engine c = new Engine("ExtendingProperties"); SignatureAspect aspect = new SignatureAspect("PropertyAdder", typeof(Foo), new Type[] { }, new IPointcut[] { }); TypeExtender extender = new TypeExtender(); ExtendedProperty property1 = new ExtendedProperty(); property1.Name = "MyIntProperty"; property1.FieldName = "_MyIntProperty"; property1.Type = typeof(int); extender.Members.Add(property1); ExtendedProperty property2 = new ExtendedProperty(); property2.Name = "MyStringProperty"; property2.FieldName = "_MyStringProperty"; property2.Type = typeof(string); extender.Members.Add(property2); aspect.TypeExtenders.Add(extender); c.Configuration.Aspects.Add(aspect); Foo proxy = (Foo)c.CreateProxy(typeof(Foo)); PropertyInfo property1Info = proxy.GetType().GetProperty("MyIntProperty"); PropertyInfo property2Info = proxy.GetType().GetProperty("MyStringProperty"); Assert.IsNotNull(property1, "Property1 was not emitted"); Assert.IsNotNull(property2, "Property2 was not emitted"); property1Info.SetValue(proxy, 123, null); int resInt = (int)property1Info.GetValue(proxy, null); Assert.IsTrue(resInt == 123, "Property1 does not hold the correct value"); property2Info.SetValue(proxy, "Hello", null); string resString = (string)property2Info.GetValue(proxy, null); Assert.IsTrue(resString == "Hello", "Property2 does not hold the correct value"); }