NAnt.Core.Element.InitializeBuildElement C# (CSharp) Method

InitializeBuildElement() public static method

public static InitializeBuildElement ( Element parent, XmlNode childNode, Element buildElement, Type elementType ) : Element
parent Element
childNode System.Xml.XmlNode
buildElement Element
elementType System.Type
return Element
        public static Element InitializeBuildElement(Element parent, XmlNode childNode, Element buildElement, Type elementType)
        {
            // if subtype of DataTypeBase
            DataTypeBase dataType = buildElement as DataTypeBase;

            if (dataType != null && dataType.CanBeReferenced && childNode.Attributes["refid"] != null ) {
                dataType.RefID = childNode.Attributes["refid"].Value;

                if (!StringUtils.IsNullOrEmpty(dataType.ID)) {
                    // throw exception because of id and ref
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1183")),
                        parent.Project.LocationMap.GetLocation(childNode));
                }

                if (parent.Project.DataTypeReferences.Contains(dataType.RefID)) {
                    dataType = parent.Project.DataTypeReferences[dataType.RefID];
                    // clear any instance specific state
                    dataType.Reset();
                } else {
                    // reference not found exception
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1184"), dataType.Name, dataType.RefID),
                        parent.Project.LocationMap.GetLocation(childNode));
                }
                if (!elementType.IsAssignableFrom(dataType.GetType())) {
                    // see if we have a valid copy constructor
                    ConstructorInfo constructor = elementType.GetConstructor(new Type[] {dataType.GetType()});
                    if (constructor != null) {
                        dataType = (DataTypeBase) constructor.Invoke(new object[] {dataType});
                    } else {
                        ElementNameAttribute dataTypeAttr = (ElementNameAttribute)
                            Attribute.GetCustomAttribute(dataType.GetType(), typeof(ElementNameAttribute));
                        ElementNameAttribute elementTypeAttr = (ElementNameAttribute)
                            Attribute.GetCustomAttribute(elementType, typeof(ElementNameAttribute));

                        // throw error wrong type definition
                        throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                            ResourceUtils.GetString("NA1185"),
                            dataTypeAttr.Name, elementTypeAttr.Name),
                            parent.Project.LocationMap.GetLocation(childNode));
                    }
                }
                // re-initialize the object with current context
                dataType.Project = parent.Project;
                dataType.Parent = parent;
                dataType.NamespaceManager = parent.NamespaceManager;
                dataType.Location = parent.Project.LocationMap.GetLocation(childNode);

                // return initialized data type
                return dataType;
            } else {
                // initialize the object with context
                buildElement.Project = parent.Project;
                buildElement.Parent = parent;
                buildElement.NamespaceManager = parent.NamespaceManager;

                // initialize element from XML
                buildElement.Initialize(childNode);

                // return initialize build element
                return buildElement;
            }
        }