Exemplo n.º 1
0
        // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------

        /// <summary>
        /// Creates a new instance of the <see cref="ByamlObjectInfo"/> class caching information on the given
        /// <paramref name="type"/>.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> to cache information on.</param>
        internal ByamlObjectInfo(Type type)
        {
            Members = new Dictionary <string, ByamlMemberInfo>();

            TypeInfo typeInfo = type.GetTypeInfo();

            if (typeInfo.GetCustomAttribute <ByamlObjectAttribute>(true) == null)
            {
                throw new ByamlException(
                          $"Type {type.Name} requires to be decorated with a {nameof(ByamlObjectAttribute)} to be used for serialization.");
            }

            // Check if the type implements IByamlSerializable.
            ImplementsByamlSerializable = typeof(IByamlSerializable).GetTypeInfo().IsAssignableFrom(type);

            // Go through all fields decorated with the ByamlValueAttribute and cache them.
            foreach (FieldInfo fieldInfo in typeInfo.GetFields(BindingFlags.Public | BindingFlags.NonPublic
                                                               | BindingFlags.Instance))
            {
                ByamlMemberAttribute attribute = fieldInfo.GetCustomAttribute <ByamlMemberAttribute>();
                if (attribute != null)
                {
                    string key = attribute.Key ?? fieldInfo.Name;
                    Members.Add(key, new ByamlMemberInfo(attribute, fieldInfo));
                }
            }
            // Go through all properties decorated with the ByamlValueAttribute and cache them.
            foreach (PropertyInfo propertyInfo in typeInfo.GetProperties(BindingFlags.Public | BindingFlags.NonPublic
                                                                         | BindingFlags.Instance))
            {
                ByamlMemberAttribute attribute = propertyInfo.GetCustomAttribute <ByamlMemberAttribute>();
                if (attribute != null)
                {
                    // Property requires to have a getter and setter.
                    if (!propertyInfo.CanRead || !propertyInfo.CanWrite)
                    {
                        throw new ByamlException(
                                  $"Property {type.Name}.{propertyInfo.Name} requires both a getter and setter to be used for serialization.");
                    }
                    string key = attribute.Key ?? propertyInfo.Name;
                    Members.Add(key, new ByamlMemberInfo(attribute, propertyInfo));
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a new instance of the <see cref="ByamlMemberInfo"/> class caching information on the given
 /// <paramref name="propertyInfo"/> decorated with the given <paramref name="attribute"/>.
 /// </summary>
 /// <param name="attribute">The <see cref="ByamlMemberAttribute"/> to cache information on.</param>
 /// <param name="propertyInfo">The <see cref="PropertyInfo"/> decorated with the attribute.</param>
 internal ByamlMemberInfo(ByamlMemberAttribute attribute, PropertyInfo propertyInfo)
 {
     Optional   = attribute.Optional;
     MemberInfo = propertyInfo;
     Type       = propertyInfo.PropertyType;
 }
Exemplo n.º 3
0
        // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------

        /// <summary>
        /// Creates a new instance of the <see cref="ByamlMemberInfo"/> class caching information on the given
        /// <paramref name="fieldInfo"/> decorated with the given <paramref name="attribute"/>.
        /// </summary>
        /// <param name="attribute">The <see cref="ByamlMemberAttribute"/> to cache information on.</param>
        /// <param name="fieldInfo">The <see cref="FieldInfo"/> decorated with the attribute.</param>
        internal ByamlMemberInfo(ByamlMemberAttribute attribute, FieldInfo fieldInfo)
        {
            Optional   = attribute.Optional;
            MemberInfo = fieldInfo;
            Type       = fieldInfo.FieldType;
        }