Пример #1
0
        /// <summary> Builds a list of names of the specified type's members that are arguments to the constructor. </summary>
        private List <string> BuildConstructorParamSources(Type type)
        {
            PublishDefaultConstructorAttribute constructorAttribute = (PublishDefaultConstructorAttribute)ReflectionUtility.GetAttribute(type, typeof(PublishDefaultConstructorAttribute));

            if ((constructorAttribute != null) && (constructorAttribute.ConstructorSignature != String.Empty))
            {
                List <string>          result     = new List <string>();
                ParameterInfo[]        parameters = ReflectionUtility.FindConstructor(constructorAttribute.ConstructorSignature, type).GetParameters();
                PublishSourceAttribute source;
                foreach (ParameterInfo parameter in parameters)
                {
                    source = (PublishSourceAttribute)ReflectionUtility.GetAttribute(parameter, typeof(PublishSourceAttribute));
                    if ((source != null) && (source.MemberName != String.Empty))
                    {
                        result.Add(source.MemberName);
                    }
                }
                return(result);
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        /// <summary> Deserializes an instance from a node. </summary>
        /// <param name="node"> The XML node.  This node will not be affected by this method. </param>
        /// <param name="instance"> An optional instance to deserialize "into". </param>
        /// <returns> The instance that was passed by AInstance or was constructed if null was passed. </returns>
        private object ReadObject(XElement node, object instance)
        {
            string elementName = node.Name.LocalName.Substring(node.Name.LocalName.LastIndexOf('.') + 1).ToLower();             // simple type name

            Type type;

            if (instance != null)               // instance provided
            {
                type = instance.GetType();
                string typeName = GetElementName(type);                 // result is lower case
                if (elementName != typeName)
                {
                    Errors.Add(new BOPException(BOPException.Codes.TypeNameMismatch, elementName, typeName));
                }
            }
            else                // construct instance
            {
                type = GetClassType(elementName, node.Name.NamespaceName);

                try
                {
                    if (!IsValueType(type))
                    {
                        PublishDefaultConstructorAttribute constructorAttribute = (PublishDefaultConstructorAttribute)ReflectionUtility.GetAttribute(type, typeof(PublishDefaultConstructorAttribute));
                        if ((constructorAttribute == null) || (constructorAttribute.ConstructorSignature == String.Empty))
                        {
                            instance = Activator.CreateInstance(type, new object[] { });
                        }
                        else
                        {
                            // create a copy of the node to work with
                            // so that the original is not corrupted by disappearing attributes
                            node     = new XElement(node);
                            instance = ConstructInstance(constructorAttribute.ConstructorSignature, type, node);
                        }
                    }
                    else
                    {
                        return(ReflectionUtility.StringToValue(node.Attribute("value").Value, type));
                    }
                }
                catch (Exception E)
                {
                    throw new BOPException(BOPException.Codes.UnableToConstruct, E, type.FullName);
                }
            }

            MemberInfo member;
            object     memberInstance;
            string     memberName;
            Type       memberType;

            // Have Type and Instance, now read the properties
            try
            {
                // Read attributes
                foreach (XAttribute attribute in node.Attributes())
                {
                    try
                    {
                        memberName = attribute.Name.LocalName.ToLower();
                        if
                        (
                            (!attribute.IsNamespaceDeclaration)
                            &&
                            !(
                                IsBOPNode(attribute.Name) &&
                                (memberName.StartsWith(BOPType) || memberName.StartsWith(BOPDefault))
                                )
                        )
                        {
                            if (IsBOPNode(attribute.Name))
                            {
                                memberInstance = instance;
                                if (Persistence.XNamesEqual(attribute.Name, XmlBOPName))
                                {
                                    member = GetNameMemberInfo(type);
                                    if (member == null)
                                    {
                                        throw new BOPException(BOPException.Codes.InvalidElementName, type.Name);
                                    }
                                }
                                else
                                {
                                    throw new BOPException(BOPException.Codes.InvalidAttribute, attribute.Name);
                                }
                            }
                            else
                            {
                                memberInstance = FindMemberInstance(instance, ref memberName);
                                member         = ReflectionUtility.FindSimpleMember(memberInstance.GetType(), memberName);
                            }
                            memberType = GetMemberType(node, member);

                            ReflectionUtility.SetMemberValue
                            (
                                member,
                                memberInstance,
                                (
                                    AttributeToValue
                                    (
                                        attribute.Value,
                                        memberType,
                                        memberInstance,
                                        member
                                    )
                                )
                            );
                        }
                    }
                    catch (Exception exception)
                    {
                        Errors.Add(exception);
                    }
                }

                // Add this instance to the list of read instances if it has a name
                member = GetNameMemberInfo(instance.GetType());
                if (member != null)
                {
                    memberName = (string)ReflectionUtility.GetMemberValue(member, instance);
                    if (memberName != String.Empty)
                    {
                        InstancesByName.Add(memberName, instance);
                    }
                }

                // read child nodes
                IList list;
                foreach (XElement localNode in node.Elements())
                {
                    try
                    {
                        memberName     = localNode.Name.LocalName.ToLower();
                        memberInstance = FindMemberInstance(instance, ref memberName);

                        // First see if the member instance has a default list attribute and use it if it does
                        string defaultListName = GetDefaultListMemberName(memberInstance.GetType());
                        if (defaultListName == String.Empty)
                        {
                            list = memberInstance as IList;
                        }
                        else                         // if no default list, assume the member instance IS the list
                        {
                            list = ReflectionUtility.GetMemberValue
                                   (
                                ReflectionUtility.FindSimpleMember(memberInstance.GetType(), defaultListName),
                                memberInstance
                                   ) as IList;
                        }
                        if (list == null)
                        {
                            throw new BOPException(BOPException.Codes.DefaultListNotFound, memberInstance.GetType().Name);
                        }
                        list.Add(ReadObject(localNode, null));
                    }
                    catch (Exception exception)
                    {
                        Errors.Add(exception);
                    }
                }

                // call AfterDeserialize
                if (instance is IBOPSerializationEvents)
                {
                    ((IBOPSerializationEvents)instance).AfterDeserialize(this);
                }

                if (AfterDeserialized != null)
                {
                    AfterDeserialized(instance);
                }
            }
            catch
            {
                if ((instance != null) && (instance is IDisposable))
                {
                    ((IDisposable)instance).Dispose();
                }
                throw;
            }

            return(instance);
        }