コード例 #1
0
        private void CacheMember(
            Type declaringType,
            Type memberType,
            MemberInfo memberInfo,
            Dictionary <string, BinaryPropertyInfo> cache,
            ref Dictionary <string, MemberInfo> ignoredMembers)
        {
            BinaryPropertyInfo binaryPropertyInfo = AddProperty(TypeMap, memberInfo, memberType, declaringType, Options);

            Debug.Assert(binaryPropertyInfo.NameAsString != null);

            string memberName = memberInfo.Name;

            // The BinaryPropertyNameAttribute or naming policy resulted in a collision.
            if (!cache.TryAdd(binaryPropertyInfo.NameAsString, binaryPropertyInfo))
            {
                BinaryPropertyInfo other = cache[binaryPropertyInfo.NameAsString];

                if (other.IsIgnored)
                {
                    // Overwrite previously cached property since it has [BinaryIgnore].
                    cache[binaryPropertyInfo.NameAsString] = binaryPropertyInfo;
                }
                else if (
                    // Does the current property have `BinaryIgnoreAttribute`?
                    !binaryPropertyInfo.IsIgnored &&
                    // Is the current property hidden by the previously cached property
                    // (with `new` keyword, or by overriding)?
                    other.MemberInfo !.Name != memberName &&
                    // Was a property with the same CLR name was ignored? That property hid the current property,
                    // thus, if it was ignored, the current property should be ignored too.
                    ignoredMembers?.ContainsKey(memberName) != true)
                {
                    // We throw if we have two public properties that have the same property name, and neither have been ignored.
                    ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameConflict(Type, binaryPropertyInfo);
                }
                // Ignore the current property.
            }
            else
            {
                int seq = System.Threading.Interlocked.Increment(ref _propertySeq);
                binaryPropertyInfo.Seq = (ushort)seq;
            }

            if (binaryPropertyInfo.IsIgnored)
            {
                (ignoredMembers ??= new Dictionary <string, MemberInfo>()).Add(memberName, memberInfo);
            }
        }