Exemplo n.º 1
0
            public ConditionalConfigurator(ConditionalElement element, XmlNode elementNode, PropertyDictionary properties, FrameworkInfo targetFramework) :
                base(element, elementNode, properties, targetFramework)
            {
                Type currentType = element.GetType();

                PropertyInfo ifdefined = currentType.GetProperty("IfDefined",
                                                                 BindingFlags.NonPublic | BindingFlags.Instance);

                InitializeAttribute(ifdefined);

                if (!element.IfDefined)
                {
                    _enabled = false;
                }
                else
                {
                    PropertyInfo unlessDefined = currentType.GetProperty(
                        "UnlessDefined",
                        BindingFlags.NonPublic | BindingFlags.Instance);
                    InitializeAttribute(unlessDefined);
                    _enabled = !element.UnlessDefined;
                }

                if (!_enabled)
                {
                    // since we will not be processing other attributes or
                    // child nodes, clear these collections to avoid
                    // errors for unrecognized attributes/elements
                    UnprocessedAttributes.Clear();
                    UnprocessedChildNodes.Clear();
                }
            }
Exemplo n.º 2
0
            protected override bool InitializeBuildElementCollection(System.Reflection.PropertyInfo propertyInfo)
            {
                Type elementType = typeof(Filter);

                BuildElementArrayAttribute buildElementArrayAttribute = (BuildElementArrayAttribute)
                                                                        Attribute.GetCustomAttribute(propertyInfo, typeof(BuildElementArrayAttribute));

                if (buildElementArrayAttribute == null || propertyInfo.PropertyType != typeof(FilterCollection))
                {
                    return(base.InitializeBuildElementCollection(propertyInfo));
                }

                XmlNodeList collectionNodes = ElementXml.ChildNodes;

                // create new array of the required size - even if size is 0
                ArrayList list = new ArrayList(collectionNodes.Count);

                foreach (XmlNode childNode in collectionNodes)
                {
                    // skip non-nant namespace elements and special elements like comments, pis, text, etc.
                    if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant")))
                    {
                        continue;
                    }

                    // remove element from list of remaining items
                    UnprocessedChildNodes.Remove(childNode.Name);

                    // initialize child element (from XML or data type reference)
                    Filter filter = TypeFactory.CreateFilter(childNode,
                                                             Element);

                    list.Add(filter);
                }

                MethodInfo addMethod = null;

                // get array of public instance methods
                MethodInfo[] addMethods = propertyInfo.PropertyType.GetMethods(BindingFlags.Public | BindingFlags.Instance);

                // search for a method called 'Add' which accepts a parameter
                // to which the element type is assignable
                foreach (MethodInfo method in addMethods)
                {
                    if (method.Name == "Add" && method.GetParameters().Length == 1)
                    {
                        ParameterInfo parameter = method.GetParameters()[0];
                        if (parameter.ParameterType.IsAssignableFrom(elementType))
                        {
                            addMethod = method;
                            break;
                        }
                    }
                }

                if (addMethod == null)
                {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           ResourceUtils.GetString("NA1020"), elementType.FullName,
                                                           propertyInfo.PropertyType.FullName, propertyInfo.Name, Name),
                                             Location);
                }

                // if value of property is null, create new instance of collection
                object collection = propertyInfo.GetValue(Element, BindingFlags.Default, null, null, CultureInfo.InvariantCulture);

                if (collection == null)
                {
                    if (!propertyInfo.CanWrite)
                    {
                        throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                               ResourceUtils.GetString("NA1093"),
                                                               buildElementArrayAttribute.Name, Name),
                                                 Location);
                    }
                    object instance = Activator.CreateInstance(
                        propertyInfo.PropertyType, BindingFlags.Public | BindingFlags.Instance,
                        null, null, CultureInfo.InvariantCulture);
                    propertyInfo.SetValue(Element, instance,
                                          BindingFlags.Default, null, null, CultureInfo.InvariantCulture);
                }

                // add each element of the arraylist to collection instance
                foreach (object childElement in list)
                {
                    addMethod.Invoke(collection, BindingFlags.Default, null, new object[] { childElement }, CultureInfo.InvariantCulture);
                }

                return(true);
            }