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"); }
public void CallServicedComponentClass() { Engine e = new Engine("CallServicedComponentClass"); IAroundInterceptor myInterceptor = new MyInterceptor(); SignatureAspect sa = new SignatureAspect("labb", "*", "*", myInterceptor); e.Configuration.Aspects.Add(sa); DummyServiced proxy = e.CreateProxy <DummyServiced>(); proxy.Foo(); }
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"); }
/// <summary> /// return a configured <c>IEngine</c> from an xml element. /// </summary> /// <param name="xmlRoot">xml node to deserialize</param> /// <returns>a configured <c>IEngine</c></returns> public IEngine Configure(XmlElement xmlRoot) { Engine engine = new Engine("App.Config"); XmlElement o = xmlRoot; if (o == null) { return(engine); } foreach (XmlNode settingsNode in o) { if (settingsNode.Name == "aspect") { IList pointcuts = new ArrayList(); IList mixins = new ArrayList(); string aspectName = settingsNode.Attributes["name"].Value; foreach (XmlNode aspectNode in settingsNode) { if (aspectNode.Name == "pointcut") { IList interceptors = new ArrayList(); foreach (XmlNode pointcutNode in aspectNode) { if (pointcutNode.Name == "interceptor") { string typeString = pointcutNode.Attributes["type"].Value; Type interceptorType = Type.GetType(typeString); if (interceptorType == null) { throw new Exception( string.Format("Interceptor type '{0}' was not found!", typeString)); } object interceptor = Activator.CreateInstance(interceptorType); interceptors.Add(interceptor); } } IPointcut pointcut = null; if (aspectNode.Attributes["target-signature"] != null) { string targetMethodSignature = aspectNode.Attributes["target-signature"].Value; pointcut = new SignaturePointcut(targetMethodSignature, interceptors); } if (aspectNode.Attributes["target-attribute"] != null) { string attributeTypeString = aspectNode.Attributes["target-attribute"].Value; Type attributeType = Type.GetType(attributeTypeString); if (attributeType == null) { throw new Exception( string.Format("Attribute type '{0}' was not found!", attributeTypeString)); } pointcut = new AttributePointcut(attributeType, interceptors); } pointcuts.Add(pointcut); } if (aspectNode.Name == "mixin") { string typeString = aspectNode.Attributes["type"].Value; Type mixinType = Type.GetType(typeString); if (mixinType == null) { throw new Exception(string.Format("Mixin type '{0}' was not found!", typeString)); } mixins.Add(mixinType); } } IGenericAspect aspect = null; if (settingsNode.Attributes["target-signature"] != null) { string targetTypeSignature = settingsNode.Attributes["target-signature"].Value; aspect = new SignatureAspect(aspectName, targetTypeSignature, mixins, pointcuts); } if (settingsNode.Attributes["target-attribute"] != null) { string attributeTypeString = settingsNode.Attributes["target-attribute"].Value; Type attributeType = Type.GetType(attributeTypeString); aspect = new AttributeAspect(aspectName, attributeType, mixins, pointcuts); } engine.Configuration.Aspects.Add(aspect); } } return(engine); }