Example #1
0
        private object CreateObject(XElement xml, out Type type, out TypeSerializingSettings typeSettings)
        {
            var typeAlias = (string)xml.Attribute(TypeTag);

            type         = this.ParseTypeAlias(typeAlias);
            typeSettings = this.GetTypeSettings(type);
            var ret = typeSettings.SkipConstructor ? FormatterServices.GetUninitializedObject(type) : Activator.CreateInstance(type);

            this.RegisterInstance(ret, xml);
            return(ret);
        }
Example #2
0
        private XElement CreateObjectContainer(object instance, out Type type, out TypeSerializingSettings typeSettings)
        {
            type         = instance.GetType();
            typeSettings = this.GetTypeSettings(type);
            var typeAlias = this.GetTypeAlias(type);
            var ret       = new XElement(ObjectTag);

            this.RegisterXml(ret, instance);

            ret.SetAttributeValue(TypeTag, typeAlias);
            return(ret);
        }
Example #3
0
        public void AddType(TypeSerializingSettings typeSettings)
        {
            if (this.TypeDictionary.ContainsKey(typeSettings.Type))
            {
                throw new Exception($"The type {typeSettings.Type} has already been added to the TypeDictionary.");
            }

            this.TypeDictionary.Add(typeSettings.Type, typeSettings);
            this.TypeDictionary2.Add(typeSettings.Alias, typeSettings);
            foreach (var legacyName in typeSettings.LegacyNames)
            {
                this.LegacyNames.Add(legacyName, typeSettings);
            }
        }
Example #4
0
        public void AddType(Type type)
        {
            var typeAtt = type.GetCustomAttribute <SerializableTypeAttribute>();

            if (typeAtt == null)
            {
                throw new Exception("Type must have the SerializedTypeAttribute.");
            }

            var typeName = typeAtt.Alias ?? type.Name;

            if (this.TypeDictionary2.ContainsKey(typeName))
            {
                throw new Exception($"Duplicate type name {typeName}.");
            }

            var settings = new TypeSerializingSettings(typeName, type)
            {
                IsCollection     = typeAtt.IsCollection,
                SkipConstructor  = typeAtt.SkipConstructor,
                IncludeAncestors = typeAtt.IncludeAncestors,
                IsDictionary     = typeAtt.IsDictionary
            };

            if (typeAtt.LegacyName != null)
            {
                settings.LegacyNames.Add(typeAtt.LegacyName);
            }

            var fields     = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).ToList();
            var properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).ToList();

            var implicitFields = Enumerable.Empty <FieldInfo>();

            if (typeAtt.ImplicitMemberFilter.HasFlag(MemberFilter.NonPublicFields))
            {
                implicitFields = implicitFields.Concat(fields.Where(f => !f.IsPublic));
            }

            if (typeAtt.ImplicitMemberFilter.HasFlag(MemberFilter.PublicFields))
            {
                implicitFields = implicitFields.Concat(fields.Where(f => f.IsPublic));
            }

            foreach (var field in implicitFields.Where(f => !Attribute.IsDefined(f, typeof(NotSerializedAttribute)) && !Attribute.IsDefined(f, typeof(SerializedAttribute))))
            {
                settings.AddMember(new MemberSerializingSettings(field.Name, field.Name, false));
            }

            var implicitProperties = Enumerable.Empty <PropertyInfo>();

            if (typeAtt.ImplicitMemberFilter.HasFlag(MemberFilter.NonPublicProperties))
            {
                implicitProperties = implicitProperties.Concat(properties.Where(p => p.GetGetMethod() == null));
            }

            if (typeAtt.ImplicitMemberFilter.HasFlag(MemberFilter.PublicProperties))
            {
                implicitProperties = implicitProperties.Concat(properties.Where(p => p.GetGetMethod() != null));
            }

            foreach (var property in implicitProperties.Where(p => !Attribute.IsDefined(p, typeof(NotSerializedAttribute)) && !Attribute.IsDefined(p, typeof(SerializedAttribute))))
            {
                settings.AddMember(new MemberSerializingSettings(property.Name, property.Name, true));
            }

            if (typeAtt.ImplicitMemberFilter.HasFlag(MemberFilter.Explicit))
            {
                var explicitFields = fields.Where(f => Attribute.IsDefined(f, typeof(SerializedAttribute)));

                foreach (var field in explicitFields)
                {
                    var fieldAtt = field.GetCustomAttribute <SerializedAttribute>();
                    settings.AddMember(CreateMemberSettings(field.Name, false, fieldAtt));
                }

                var explicitProperties = properties.Where(f => Attribute.IsDefined(f, typeof(SerializedAttribute)));

                foreach (var property in explicitProperties)
                {
                    var propertyAtt = property.GetCustomAttribute <SerializedAttribute>();
                    settings.AddMember(CreateMemberSettings(property.Name, true, propertyAtt));
                }
            }

            this.AddType(settings);
        }