コード例 #1
0
        private void ValidatePropertyInfo(PropertyInfo prop)
        {
            // Get a possible binary member configuration or use the default one.
            var attrib    = prop.GetCustomAttribute <BinaryContentAttribute>();
            var hasAttrib = attrib != null;

            attrib ??= new BinaryContentAttribute();

            // 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 &&
                (prop.GetMethod?.IsPublic == true && prop.SetMethod?.IsPublic == true))
            {
                // Store member in a deterministic order.
                var memberData = new MemberData(prop, prop.PropertyType, attrib);
                if ((attrib.NodeType & BinaryContentTypes.Node) != 0)
                {
                    if (attrib.Order == Int32.MinValue)
                    {
                        UnorderedNodes.Add(attrib.Name ?? prop.Name, memberData);
                    }
                    else
                    {
                        OrderedNodes.Add(attrib.Order, memberData);
                    }
                }
                else if ((attrib.NodeType & BinaryContentTypes.Attribute) != 0)
                {
                    if (attrib.Order == Int32.MinValue)
                    {
                        UnorderedAttributes.Add(attrib.Name ?? prop.Name, memberData);
                    }
                    else
                    {
                        OrderedAttributes.Add(attrib.Order, memberData);
                    }
                }
            }
        }
コード例 #2
0
        private void ValidateFieldInfo(FieldInfo field)
        {
            // Get a possible binary member configuration or use the default one.
            var attrib    = field.GetCustomAttribute <BinaryContentAttribute>();
            var hasAttrib = attrib != null;

            attrib ??= new BinaryContentAttribute();

            // Field must be decorated or public.
            if (hasAttrib && field.IsPublic)
            {
                // Store member in a deterministic order.
                var memberData = new MemberData(field, field.FieldType, attrib);
                if ((attrib.NodeType & BinaryContentTypes.Node) != 0)
                {
                    if (attrib.Order == Int32.MinValue)
                    {
                        UnorderedNodes.Add(attrib.Name ?? field.Name, memberData);
                    }
                    else
                    {
                        OrderedNodes.Add(attrib.Order, memberData);
                    }
                }
                else if ((attrib.NodeType & BinaryContentTypes.Attribute) != 0)
                {
                    if (attrib.Order == Int32.MinValue)
                    {
                        UnorderedAttributes.Add(attrib.Name ?? field.Name, memberData);
                    }
                    else
                    {
                        OrderedAttributes.Add(attrib.Order, memberData);
                    }
                }
            }
        }