Ejemplo n.º 1
0
        /**
         * Reads all public memebers from node
         * forceNulls: If true null values will overwrite existing data.  The default behaviour is that null fields leave data as exists.
         */
        protected void AutoReadSerialize(XElement node, bool forceNulls = false)
        {
            FieldInfo[]   fields        = this.GetType().GetFields();
            List <String> ignoredFields = GetIgnoredFields();

            foreach (var field in fields)
            {
                if (field.IsStatic)
                {
                    continue;
                }
                try {
                    var attr = field.ExtendedAttributes();
                    if (attr.Ignore)
                    {
                        continue;
                    }
                    if (ignoredFields.Contains(field.Name))
                    {
                        continue;
                    }

                    object value = null;

                    switch (attr.ReferenceType)
                    {
                    case FieldReferenceType.Full:
                        value = ReadObject(node, attr.DisplayName ?? field.Name, field.FieldType);
                        break;

                    case FieldReferenceType.ID:
                    case FieldReferenceType.Name:
                        if (!field.FieldType.IsSubclassOf(typeof(NamedDataObject)))
                        {
                            throw new Exception("Referenced types must be NamedDataObjects");
                        }

                        var library = NamedDataObject.GetLibraryForType(field.FieldType);

                        if (library == null)
                        {
                            throw new Exception("No global library found for referenced type [" + field.FieldType + "]");
                        }

                        value = ReadObject(node, attr.DisplayName ?? field.Name, field.FieldType, attr.ReferenceType, library);
                        break;
                    }
                    if ((value != null) || forceNulls)
                    {
                        field.SetValue(this, value);
                    }
                } catch (Exception e) {
                    Trace.LogWarning("Error reading object [" + field.Name + "] " + e.Message + "\n" + node);
                }
            }
        }
Ejemplo n.º 2
0
        /** Reads a list of dataobjects from this nodes child of given name, returns null if not found.  Can be referenced, or full. */
        protected List <T> ReadNamedDataObjectList <T>(XElement node, string name, FieldReferenceType referenceType, DataLibrary <T> library = null) where T : NamedDataObject
        {
            node = node.Element(name);

            if (node == null)
            {
                return(null);
            }

            var    list        = new List <T>();
            string subNodeName = GetClassAttributesForType(typeof(T)).NodeName;

            library = library ?? NamedDataObject.GetLibraryForType <T>();

            if (library == null && referenceType != FieldReferenceType.Full)
            {
                throw new Exception("Can not load type " + typeof(T) + " as no default library is defined, and it is stored referenced.");
            }

            foreach (var subNode in node.Elements(subNodeName))
            {
                T item = null;

                switch (referenceType)
                {
                case FieldReferenceType.Full:
                    item = (T)Activator.CreateInstance(typeof(T));
                    item.ReadNode(subNode);
                    break;

                case FieldReferenceType.ID:
                    var fieldId = int.Parse(subNode.Value);
                    item = (T)library._byID(fieldId);
                    break;

                case FieldReferenceType.Name:
                    var fieldName = subNode.Value;
                    item = (T)library._byName(fieldName);
                    break;
                }

                list.Add(item);
            }

            return(list);
        }