Пример #1
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);
                }
            }
        }