예제 #1
0
        /// <summary>
        /// Creates an object from the specified root element.
        /// </summary>
        /// <param name="state">The current loader state.</param>
        /// <param name="type">The type of object to create.</param>
        /// <param name="element">The element from which to create an object.</param>
        /// <returns>The object that was created.</returns>
        private Object CreateObjectFromRootElement(ObjectLoaderState state, Type type, XElement element)
        {
            // First, ensure that we have a class, key, and identifier.
            var objClassName = state.ResolveClass((String)element.Attribute("Class"));

            if (String.IsNullOrEmpty(objClassName))
            {
                throw new InvalidOperationException(NucleusStrings.DataObjectMissingClass);
            }

            // If we're loading a Nucleus data object, parse its unique key and ID.
            var argsBase = default(Object[]);

            if (typeof(DataObject).IsAssignableFrom(type))
            {
                var objKey = (String)element.Attribute("Key");
                if (String.IsNullOrEmpty(objKey))
                {
                    throw new InvalidOperationException(NucleusStrings.DataObjectMissingKey);
                }

                var objID = (String)element.Attribute("ID");
                if (String.IsNullOrEmpty(objID))
                {
                    throw new InvalidOperationException(NucleusStrings.DataObjectMissingID);
                }

                Guid objIDValue;
                if (!Guid.TryParse(objID, out objIDValue))
                {
                    throw new InvalidOperationException(NucleusStrings.DataObjectInvalidID.Format(objID));
                }

                argsBase = new Object[] { objKey, objIDValue };
            }

            // Attempt to find the object class and make sure it's of the correct type.
            var objClass = Type.GetType(objClassName, false);

            if (objClass == null || !type.IsAssignableFrom(objClass))
            {
                throw new InvalidOperationException(NucleusStrings.DataObjectInvalidClass.Format(objClassName ?? "(null)", argsBase[0]));
            }

            // Attempt to instantiate the object.
            return(CreateObject(state, objClass, argsBase, GetSpecifiedConstructorArguments(element)));
        }
예제 #2
0
        /// <summary>
        /// Gets the type defined by the specified element.
        /// </summary>
        /// <param name="state">The loader state.</param>
        /// <param name="baseType">The base type.</param>
        /// <param name="element">The element to evaluate.</param>
        /// <returns>The type defined by the specified element.</returns>
        private Type GetTypeFromElement(ObjectLoaderState state, Type baseType, XElement element)
        {
            var complexTypeAttr = element.Attribute("Type");

            if (complexTypeAttr != null && String.IsNullOrEmpty(complexTypeAttr.Value))
            {
                throw new InvalidOperationException(NucleusStrings.DataObjectInvalidType.Format(element.Name));
            }

            var complexType = (complexTypeAttr == null) ? baseType : Type.GetType(state.ResolveClass(complexTypeAttr.Value), false);

            if (complexType == null)
            {
                throw new InvalidOperationException(NucleusStrings.DataObjectInvalidType.Format(element.Name));
            }

            if (!baseType.IsAssignableFrom(complexType))
            {
                throw new InvalidOperationException(NucleusStrings.DataObjectIncompatibleType.Format(element.Name));
            }

            return(complexType);
        }