public void ReadXml(XmlReader reader)
        {
            // Cast the Data back from the Abstract Type.
            string typeAttrib = reader.GetAttribute("xsi:type");

//#if DEBUG
            if (typeAttrib == null || !MyXmlSerializerManager.IsSerializerAvailable(typeAttrib))
//#else
//            if (typeAttrib == null)
//#endif
            {
                typeAttrib = MyXmlSerializerManager.GetSerializedName(typeof(TAbstractBase));
            }

            // Read the Data, Deserializing based on the (now known) concrete type.
            {
                CustomRootReader customReader;
                ReaderPool.AllocateOrCreate(out customReader);
                customReader.Init(typeAttrib, reader);

                var serializer = MyXmlSerializerManager.GetSerializer(typeAttrib);
                this.Data = (TAbstractBase)serializer.Deserialize(customReader);

                customReader.Release();
                ReaderPool.Deallocate(customReader);
            }
        }
        public void WriteXml(XmlWriter writer)
        {
            Type type = m_data.GetType();

            {
                CustomRootWriter customWriter;
                WriterPool.AllocateOrCreate(out customWriter);
                var derivedName = MyXmlSerializerManager.GetSerializedName(type);
                customWriter.Init(derivedName, writer);

                MyXmlSerializerManager.GetSerializer(type).Serialize(customWriter, m_data);

                customWriter.Release();
                WriterPool.Deallocate(customWriter);
            }
        }
示例#3
0
        public static void RegisterTypesFromAssembly(Assembly assembly)
        {
            if (assembly == null)
            {
                return;
            }
#if !XB1 // XB1_ALLINONEASSEMBLY
            if (m_registeredAssemblies.Contains(assembly))
            {
                return;
            }
            m_registeredAssemblies.Add(assembly);

            if (m_registered.Contains(assembly))
            {
                return;
            }
            m_registered.Add(assembly);
#endif // !XB1

#if XB1 // XB1_ALLINONEASSEMBLY
            if (m_registered == true)
            {
                return;
            }
            m_registered = true;
            foreach (Type type in MyAssembly.GetTypes())
#else // !XB1
            foreach (Type type in assembly.GetTypes())
#endif // !XB1
            {
                var descriptorArray = type.GetCustomAttributes(typeof(MyDefinitionTypeAttribute), false);

                if (descriptorArray.Length > 0)
                {
                    if (!type.IsSubclassOf(typeof(MyDefinitionBase)) && type != typeof(MyDefinitionBase))
                    {
                        MyLog.Default.Error("Type {0} is not a definition.", type.Name);
                        continue;
                    }

                    foreach (MyDefinitionTypeAttribute descriptor in descriptorArray)
                    {
                        m_definitionFactory.RegisterDescriptor(descriptor, type);
                        var pp = (MyDefinitionPostprocessor)Activator.CreateInstance(descriptor.PostProcessor);
                        pp.DefinitionType = descriptor.ObjectBuilderType;
                        m_postProcessors.Add(pp);
                        m_postprocessorsByType.Add(descriptor.ObjectBuilderType, pp);
                        MyXmlSerializerManager.RegisterSerializer(descriptor.ObjectBuilderType);
                    }

                    var tp = type;
                    while (tp != typeof(MyDefinitionBase))
                    {
                        tp = tp.BaseType;

                        HashSet <Type> children;
                        if (!m_childDefinitionMap.TryGetValue(tp, out children))
                        {
                            children = new HashSet <Type>();
                            m_childDefinitionMap[tp] = children;
                            children.Add(tp); // make sure it contains itself
                        }

                        children.Add(type);
                    }
                }
            }

            m_postProcessors.Sort(MyDefinitionPostprocessor.Comparer);
        }