/// <summary>
		/// Creates cache information for a property of a serializable class
		/// </summary>
		/// <param name="attrib">Serialization attribute applied to the property</param>
		/// <param name="prop">Property metadata</param>
		public PropertySerializationInfo(
								  SerializablePropertyAttribute attrib,
								  MemberInfo prop
								  )
		{
			this.attribute = attrib;
			this.property = prop;

			DeterminePropertyType(false);
		}
		/// <summary>
		/// Constructs cache information for the indexer property of a serializable class
		/// </summary>
		/// <param name="attrib"></param>
		/// <param name="type"></param>
		public PropertySerializationInfo(
								  SerializablePropertyAttribute attrib,
								  Type type
								  )
		{
			Type icoll, idict;
			MethodInfo add, count;

			//  Verify that the type implements collection semantics
			if (FindCollectionInterface(type, out icoll, out idict, out add, out count) == false)
			{
				throw new ArgumentException(string.Format(
								"The type {0} must support ICollection<> to serialize the indexer", type.FullName
								));
			}

			this.attribute = attrib;
			this.classType = type;
			DeterminePropertyType(true);
		}
Beispiel #3
0
        private static IPropertyDescriptor ConvertPropertyDescriptor(IPropertyDescriptor propertyDescriptor)
        {
            SerializablePropertyAttribute attribute = propertyDescriptor.GetCustomAttribute <SerializablePropertyAttribute>();

            if (attribute is null)
            {
                return(null);
            }

            PropertyDescriptor result = new PropertyDescriptor(propertyDescriptor);

            if (attribute.Name != null)
            {
                result.Name = attribute.Name;
            }

            result.Order = attribute.BinaryOrder;

            return(result);
        }
		/// <summary>
		/// Creates cache information for a property of a serializable class
		/// </summary>
		/// <param name="attrib">Serialization attribute applied to the property</param>
		/// <param name="prop">Property metadata</param>
		public PropertySerializationInfo(
								  SerializablePropertyAttribute attrib,
								  MemberInfo prop
								  )
		{
			this.attribute = attrib;
			this.property = prop;

			DeterminePropertyType(false);
		}
		/// <summary>
		/// Constructs cache information for the indexer property of a serializable class
		/// </summary>
		/// <param name="attrib"></param>
		/// <param name="type"></param>
		public PropertySerializationInfo(
								  SerializablePropertyAttribute attrib,
								  Type type
								  )
		{
			Type icoll, idict;
			MethodInfo add, count;

			//  Verify that the type implements collection semantics
			if (FindCollectionInterface(type, out icoll, out idict, out add, out count) == false)
			{
				throw new ArgumentException(string.Format(
								"The type {0} must support ICollection<> to serialize the indexer", type.FullName
								));
			}

			this.attribute = attrib;
			this.classType = type;
			DeterminePropertyType(true);
		}
Beispiel #6
0
        public static PartDefinition Create(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var classAttributes = Attribute.GetCustomAttributes(type, typeof(SerializableClassAttribute), false);

            if (classAttributes == null || classAttributes.Length == 0)
            {
                throw new BarfException(string.Format("{0} does not define a {1}", type.Name, typeof(SerializableClassAttribute).Name), type, null);
            }

            var classAttribute = classAttributes
                                 .OfType <SerializableClassAttribute>()
                                 .First <SerializableClassAttribute>();

            SerializablePropertyAttribute attribute;

            if (classAttribute.SerializeBaseClass)
            {
                attribute = new SerializablePropertyAttribute(0);
            }
            else
            {
                var attributes = Attribute
                                 .GetCustomAttributes(type, typeof(SerializablePropertyAttribute))
                                 .OfType <SerializablePropertyAttribute>()
                                 .Where <SerializablePropertyAttribute>(a => !(a is SerializableInheritedPropertyAttribute))
                                 .ToArray <SerializablePropertyAttribute>();

                if (attributes.Length > 1)
                {
                    throw new BarfException(string.Format("Can't have more than one {0} defined on a type.", typeof(SerializablePropertyAttribute).Name), type, null);
                }

                if (attributes.Length == 0)
                {
                    return(null);
                }

                attribute = attributes[0];
            }

            if (attribute.Dynamic)
            {
                // todo validate better (top level type can't be but generic parameters may be)
                //throw new BarfException(string.Format("{0}'s that are applied to types may not be Dynamic. Type=\"{1}\"", typeof(SerializablePropertyAttribute).Name, type.Name), type, null);
            }

            var partType = type.BaseType;

            if (partType == typeof(object))
            {
                partType = type
                           .GetInterfaces()
                           .Where(t =>
                {
                    if (t == typeof(ICollection <>))
                    {
                        return(true);
                    }
                    return(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection <>));
                })
                           .FirstOrDefault <Type>()
                           ?? typeof(object);
            }

            return(new PartDefinition
            {
                Member = null,
                MemberName = "!base",
                Version = attribute.Version,
                ObsoleteVersion = attribute.ObsoleteVersion,
                Flags = attribute.Dynamic ? PartFlags.Dynamic : PartFlags.None,
                ReadMethod = attribute.ReadMethod,
                WriteMethod = attribute.WriteMethod,
                Type = partType
            });
        }
Beispiel #7
0
        private static IPropertyDescriptor DescriptorWithSerializablePropertyAttribute(SerializablePropertyAttribute attribute)
        {
            IPropertyDescriptor result = Substitute.For <IPropertyDescriptor>();

            result.GetCustomAttribute <SerializablePropertyAttribute>().Returns(attribute);

            return(result);
        }
        void BuildPropertyList(
            List <PropertySerializationInfo> properties,
            out int obsoleteVersion
            )
        {
            obsoleteVersion = 0;

            //  If class has SerializableProperty attribute then add Indexer
            object[] attributes = this.type.GetCustomAttributes(typeof(SerializablePropertyAttribute), false);

            foreach (SerializablePropertyAttribute attrib in attributes)
            {
                PropertySerializationInfo propInfo = null;

                if (attrib is SerializableInheritedPropertyAttribute)
                {
                    SerializableInheritedPropertyAttribute inherit = (SerializableInheritedPropertyAttribute)attrib;

                    if (this.serializableBaseType != null)
                    {
                        throw new ApplicationException(string.Format(
                                                           "The SerializableInheritedProperty attribute cannot be used with SerializeBaseClass=true ({0})",
                                                           this.type.FullName
                                                           ));
                    }

                    if (this.type.IsSubclassOf(inherit.BaseClass) == false)
                    {
                        throw new ApplicationException(string.Format(
                                                           "Invalid SerializableInheritedProperty attribute: {0} is not a base class of {1}",
                                                           this.type.Name,
                                                           inherit.BaseClass.Name
                                                           ));
                    }

                    MemberInfo[] prop = inherit.BaseClass.GetMember(
                        inherit.BasePropertyName,
                        MemberTypes.Field | MemberTypes.Property,
                        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly
                        );

                    if (prop.Length == 0)
                    {
                        throw new ApplicationException(string.Format(
                                                           "Failed to located property \"{0}\" inherited from base class \"{1}\"",
                                                           inherit.BasePropertyName,
                                                           inherit.BaseClass.Name
                                                           ));
                    }

                    propInfo = new PropertySerializationInfo(attrib, prop[0]);
                }
                else
                {
                    propInfo = new PropertySerializationInfo(attrib, this.type);
                }

                properties.Add(propInfo);
                obsoleteVersion = Math.Max(obsoleteVersion, propInfo.ObsoleteVersion);
            }

            //  Get members. We don't need to worry about the member type
            //  because only valid member types can have the SerializableProperty attribute
            foreach (MemberInfo prop in this.type.GetMembers(
                         BindingFlags.Public
                         | BindingFlags.NonPublic
                         | BindingFlags.Instance
                         | BindingFlags.DeclaredOnly
                         ))
            {
                PropertySerializationInfo     propInfo = null;
                SerializablePropertyAttribute attrib   = null;

                attrib = SerializablePropertyAttribute.GetAttribute(prop);
                if (attrib == null)
                {
                    continue;
                }

                propInfo = new PropertySerializationInfo(attrib, prop);
                properties.Add(propInfo);
                obsoleteVersion = Math.Max(obsoleteVersion, propInfo.ObsoleteVersion);
            }
        }