Example #1
0
        public static void ProcessCompositionXmlFromResource(this IComponentContext context, Assembly assembly, string configurationResourceName)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var xmlProcessingContext = new XmlProcessingContext(context);

            ProcessCompositionXmlFromResource(assembly, configurationResourceName, xmlProcessingContext);
            xmlProcessingContext.ThrowIfErrors();
        }
Example #2
0
        public static void ProcessCompositionXml(this IComponentContext context, string configurationPath)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var xmlProcessingContext = new XmlProcessingContext(context);

            ProcessCompositionXml(configurationPath, xmlProcessingContext);
            xmlProcessingContext.ThrowIfErrors();
        }
        /// <summary>
        /// Actually parses the object and returns the result. Used both in short-hand
        /// form and in direct Object element parsing.
        /// </summary>
        public static object ParseObject(string typeName, bool initializePlugs, XmlElement[] childElements,
                                         XmlProcessingContext context)
        {
            context.EnterRunningLocation(string.Format("Object({0})", typeName));

            // Look for constructor arguments.
            // Set default to null, so that Activator calls default constructor
            // in case the the "ConstructorArgs" element is not specified.

            object[] constructorArguments = null;

            foreach (var childElement in childElements)
            {
                if (childElement.Name != "ConstructorArgs")
                {
                    continue;
                }
                // Check if this is the second "ConstructorArgs" element.
                // If so, report an error and return.

                if (constructorArguments != null)
                {
                    context.ReportError("The 'ConstructorArgs' element can be specified maximum once in an 'Object' element.");
                    context.LeaveRunningLocation();
                    return(null);
                }

                constructorArguments = ParseConstructorArgs(childElement, context);
                context.ThrowIfErrors();
            }

            // Resolve the "Type" for the object to be instantiated

            var objectType = SimpleTypeParserUtil.ParseType(typeName, context);

            if (objectType == null)
            {
                context.ReportError(string.Format("Type '{0}' could not be loaded.", typeName));
                context.LeaveRunningLocation();
                return(null);
            }

            // Use Activator class to instantiate the object

            var result = Activator.CreateInstance(objectType, constructorArguments);

            if (initializePlugs)
            {
                context.ComponentContext.InitializePlugs(result, objectType);
            }

            foreach (var childElement in childElements)
            {
                switch (childElement.Name)
                {
                case "ConstructorArgs":
                    break;

                case "Property":
                    ParseObjectProperty(result, childElement, context);
                    break;

                case "Field":
                    ParseObjectField(result, childElement, context);
                    break;

                default:                                // Also: case "ConstructorArgs"
                    context.ReportError(
                        string.Format("Xml element '{0}' is not allowed in 'Object' element - type: {1}", childElement.Name,
                                      typeName));
                    context.LeaveRunningLocation();
                    return(null);
                }
            }

            context.LeaveRunningLocation();
            return(result);
        }