コード例 #1
0
        public override void WriteField(XmlWriter xmlWriter, object fieldToWrite)
        {
            object list = fieldToWrite;

            int listCount = (int)list.GetType().GetProperty("Count").GetValue(list, null);

            for (int index = 0; index < listCount; index++)
            {
                object item = list.GetType().GetMethod("get_Item").Invoke(list, new object[] { index });
                if (item is GameObject <DoubleComponent> )
                {
                    xmlWriter.WriteStartElement("Item");

                    GameDataAttribute.WriteTypeAttributes(xmlWriter, item);

                    ((GameObject <DoubleComponent>)item).WriteGameObjectData(xmlWriter);
                    xmlWriter.WriteEndElement();
                }
                else
                {
                    xmlWriter.WriteValue(item);
                    xmlWriter.WriteValue(" ");
                }
            }
        }
コード例 #2
0
        public virtual void WriteGameObjectData(XmlWriter xmlWriter)
        {
            Type gameObjectType = this.GetType();

            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            System.Reflection.FieldInfo[] fieldsOfGameObject = gameObjectType.GetFields(bindingFlags);
            foreach (FieldInfo fieldOfGameObject in fieldsOfGameObject)
            {
                object[] gameDataAttributes = fieldOfGameObject.GetCustomAttributes(typeof(GameDataAttribute), false);
                if (gameDataAttributes.Length > 0)
                {
                    if (gameDataAttributes.Length > 1)
                    {
                        throw new Exception("You can only have one GameDataAttribute on any given Field.");
                    }

                    object objectWithAttribute = fieldOfGameObject.GetValue(this);
                    if (objectWithAttribute != null)
                    {
                        GameDataAttribute singleGameDataAttribute = (GameDataAttribute)gameDataAttributes[0];
                        String            Name = singleGameDataAttribute.Name;

                        if (Name.Contains(" "))
                        {
                            throw new Exception(this.ToString() + " : '" + Name + "' has a space. Attribute names con not contain spaces.");
                        }

                        xmlWriter.WriteStartElement(Name);
                        GameDataAttribute.WriteTypeAttributes(xmlWriter, objectWithAttribute);
                        xmlWriter.WriteEndAttribute();

                        if (objectWithAttribute == null)
                        {
                            throw new Exception(this.ToString() + " : " + fieldOfGameObject.ToString() + " must have a default Value.\n"
                                                + "\n"
                                                + "All data marked as [GameData] must be a primitive or a struct or if a class have a DEFALT Value or filed initializer.");
                        }

                        if (objectWithAttribute is GameObject <T> )
                        {
                            ((GameObject <T>)objectWithAttribute).WriteGameObjectData(xmlWriter);
                        }
                        else
                        {
                            singleGameDataAttribute.WriteField(xmlWriter, objectWithAttribute);
                        }
                        xmlWriter.WriteEndElement();
                    }
                }
            }
        }