public void CanApplyConstructorArgsToAbstractType() { IResource resource = new ReadOnlyXmlTestResource("ctor-args.xml", GetType()); XmlObjectFactory xof = new XmlObjectFactory(resource); TestObject rod = (TestObject)xof.GetObject("rod"); Assert.AreEqual(1, rod.Age); RootObjectDefinition def = (RootObjectDefinition) xof.GetObjectDefinition("rod"); ConstructorResolver resolver = new ConstructorResolver(xof, xof, new SimpleInstantiationStrategy(), new ObjectDefinitionValueResolver(xof)); ConstructorInstantiationInfo ci = resolver.GetConstructorInstantiationInfo("rod", def, null, null); AbstractObjectDefinition objDef = (AbstractObjectDefinition)xof.GetObjectDefinition("foo"); objDef.IsAbstract = false; TestObject foo = (TestObject) xof.GetObject("foo"); Assert.AreEqual(2, foo.Age); }
/// <summary> /// Applies attributes to the proxy class. /// </summary> /// <param name="typeBuilder">The type builder to use.</param> /// <param name="targetType">The proxied class.</param> /// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/> /// <see cref="IProxyTypeBuilder.TypeAttributes"/> protected override void ApplyTypeAttributes(TypeBuilder typeBuilder, Type targetType) { foreach (object attr in GetTypeAttributes(targetType)) { if (attr is CustomAttributeBuilder) { typeBuilder.SetCustomAttribute((CustomAttributeBuilder)attr); } #if NET_2_0 else if (attr is CustomAttributeData) { typeBuilder.SetCustomAttribute( ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr)); } #endif else if (attr is Attribute) { typeBuilder.SetCustomAttribute( ReflectionUtils.CreateCustomAttribute((Attribute)attr)); } else if (attr is IObjectDefinition) { RootObjectDefinition objectDefinition = (RootObjectDefinition) attr; //TODO check that object definition is for an Attribute type. //Change object definition so it can be instantiated and make prototype scope. objectDefinition.IsAbstract = false; objectDefinition.IsSingleton = false; string objectName = ObjectDefinitionReaderUtils.GenerateObjectName(objectDefinition, objectFactory); objectFactory.RegisterObjectDefinition(objectName, objectDefinition); //find constructor and constructor arg values to create this attribute. ConstructorResolver constructorResolver = new ConstructorResolver(objectFactory, objectFactory, new SimpleInstantiationStrategy(), new ObjectDefinitionValueResolver(objectFactory)); ConstructorInstantiationInfo ci = constructorResolver.GetConstructorInstantiationInfo(objectName, objectDefinition, null, null); if (objectDefinition.PropertyValues.PropertyValues.Length == 0) { CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo, ci.ArgInstances); typeBuilder.SetCustomAttribute(cab); } else { object attributeInstance = objectFactory.GetObject(objectName); IObjectWrapper wrappedAttributeInstance = new ObjectWrapper(attributeInstance); PropertyInfo[] namedProperties = wrappedAttributeInstance.GetPropertyInfos(); object[] propertyValues = new object[namedProperties.Length]; for (int i = 0; i < namedProperties.Length; i++) { propertyValues[i] = wrappedAttributeInstance.GetPropertyValue(namedProperties[i].Name); } CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo, ci.ArgInstances, namedProperties, propertyValues); typeBuilder.SetCustomAttribute(cab); } } } }