Ejemplo n.º 1
0
        // ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------

        private void ValidateFieldInfo(FieldInfo field)
        {
            // Get a possible binary member configuration or use the default one.
            BinaryMemberAttribute attrib = field.GetCustomAttribute <BinaryMemberAttribute>();
            bool hasAttrib = attrib != null;

            attrib = attrib ?? new BinaryMemberAttribute();

            // Field must be decorated or public.
            if (hasAttrib || (!Attribute.Explicit && field.IsPublic))
            {
                // For fields of enumerable type ElementCount must be specified.
                if (field.FieldType.IsEnumerable() && attrib.Length <= 0)
                {
                    throw new InvalidOperationException(
                              $"Field {field} requires an element count specified with a {nameof(BinaryMemberAttribute)}.");
                }

                // Store member in a deterministic order.
                MemberData memberData = new MemberData(field, field.FieldType, attrib);
                if (attrib.Order == Int32.MinValue)
                {
                    UnorderedMembers.Add(field.Name, memberData);
                }
                else
                {
                    OrderedMembers.Add(attrib.Order, memberData);
                }
            }
        }
Ejemplo n.º 2
0
        private void ValidatePropertyInfo(PropertyInfo prop)
        {
            // Get a possible binary member configuration or use the default one.
            BinaryMemberAttribute attrib = prop.GetCustomAttribute <BinaryMemberAttribute>();
            bool hasAttrib = attrib != null;

            attrib = attrib ?? new BinaryMemberAttribute();

            // Property must have getter and setter - if not, throw an exception if it is explicitly decorated.
            if (hasAttrib && (prop.GetMethod == null || prop.SetMethod == null))
            {
                throw new InvalidOperationException($"Getter and setter on property {prop} not found.");
            }
            // Property must be decorated or getter and setter public.
            if (hasAttrib ||
                (!Attribute.Explicit && prop.GetMethod?.IsPublic == true && prop.SetMethod?.IsPublic == true))
            {
                // For properties of enumerable type ElementCount must be specified.
                if (prop.PropertyType.IsEnumerable() && attrib.Length <= 0)
                {
                    throw new InvalidOperationException(
                              $"Property {prop} requires an element count specified with a {nameof(BinaryMemberAttribute)}.");
                }

                // Store member in a deterministic order.
                MemberData memberData = new MemberData(prop, prop.PropertyType, attrib);
                if (attrib.Order == Int32.MinValue)
                {
                    UnorderedMembers.Add(prop.Name, memberData);
                }
                else
                {
                    OrderedMembers.Add(attrib.Order, memberData);
                }
            }
        }