Example #1
0
        public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();
            using var document = JsonDocument.Parse(JsonSerializer.Serialize(value));
            var valueType = typeof(T);

            var variableSettings = new VariableInfoDescriptor()
            {
                Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
            };

            foreach (var property in document.RootElement.EnumerateObject())
            {
                var memberInfo = valueType.GetVariableMember(property.Name, variableSettings);

                if (memberInfo?.DeclaringType == null || !variablesInclusionHelper.IsVariableConsidered(memberInfo.DeclaringType, property.Name))
                {
                    continue;
                }

                property.WriteTo(writer);
            }

            writer.WriteEndObject();
        }
        public static bool IsVariable(this MemberInfo memberInfo, VariableInfoDescriptor variableInfoDescriptor)
        {
            if (!MemberInfoTools.IsVariable(memberInfo))
            {
                return(false);
            }

            var isIncludedByAttributeTypes  = variableInfoDescriptor.IncludeByAttributeTypes == null || variableInfoDescriptor.IncludeByAttributeTypes.All(attrType => memberInfo.IsDefined(attrType, variableInfoDescriptor.IncludeByAttributeTypesInherit));
            var notExcludedByAttributeTypes = !(variableInfoDescriptor.ExcludeByAttributeTypes != null && variableInfoDescriptor.ExcludeByAttributeTypes.Any(attrType => memberInfo.IsDefined(attrType, variableInfoDescriptor.ExcludeByAttributeTypesInherit)));

            if (memberInfo is FieldInfo fieldInfo)
            {
                return(isIncludedByAttributeTypes && notExcludedByAttributeTypes);
            }
            else if (memberInfo is PropertyInfo propertyInfo)
            {
                return((!variableInfoDescriptor.IncludeIfReadable || propertyInfo.CanRead) &&
                       (!variableInfoDescriptor.IncludeIfWritable || propertyInfo.CanWrite) &&
                       isIncludedByAttributeTypes &&
                       !(variableInfoDescriptor.ExcludeIfReadable && propertyInfo.CanRead) &&
                       !(variableInfoDescriptor.ExcludeIfWritable && propertyInfo.CanWrite) &&
                       notExcludedByAttributeTypes);
            }

            return(false);
        }
        public SingleTypePropertyCache(INotifyPropertyChanged singleTypedPropertyNotifier, object singleTypedPropertiesOwner, IEqualityComparer <PropertyType>?propertyValueEqualityComparer)
        {
            singleTypedPropertyNotifier = singleTypedPropertyNotifier
                                          ?? throw new ArgumentNullException(nameof(singleTypedPropertyNotifier));

            singleTypedPropertiesOwner = singleTypedPropertiesOwner
                                         ?? throw new ArgumentNullException(nameof(singleTypedPropertiesOwner));

            propertyValueEqualityComparer = propertyValueEqualityComparer
                                            ?? EqualityComparer <PropertyType> .Default;

            TrackingPropertyType = typeof(PropertyType);
            var propertyTypeInfo = TrackingPropertyType.GetTypeInfo();

            if (propertyTypeInfo.IsValueType)
            {
                propertyComparisonMode = PropertyComparisonMode.ValueType;
            }
            else
            {
                propertyComparisonMode = PropertyComparisonMode.ReferenceType;
            }

            cachedPropertyValues         = new Dictionary <string, PropertyType>();
            CachedPropertyValues         = new ReadOnlyDictionary <string, PropertyType>(cachedPropertyValues);
            VariableInfoDescriptor       = new VariableInfoDescriptor();
            TrackingPropertyDefaultValue = default;
            CanHandleDefaultValue        = true;

            SingleTypedPropertyNotifier = singleTypedPropertyNotifier;
            SingleTypedPropertyNotifier.PropertyChanged += SingleTypePropertyNotifier_PropertyChanged;
            SingleTypedPropertiesOwner     = singleTypedPropertiesOwner;
            SingleTypedPropertiesOwnerType = singleTypedPropertiesOwner.GetType();
            PropertyValueEqualityComparer  = propertyValueEqualityComparer;
        }
Example #4
0
        public static TargetType ShallowCopy <SourceType, TargetType>(SourceType source)
            where TargetType : notnull
            where SourceType : notnull
        {
            var flags = VariableInfoDescriptor.DefaultFlags | BindingFlags.NonPublic;

            var cloningObjectMembersSettings = new VariableInfoDescriptor()
            {
                Flags             = flags,
                IncludeIfWritable = true,
            };

            var declaredType = typeof(TargetType);

            var cloningObjectMembersByNameList = declaredType
                                                 .GetVariableMembers(descriptor: cloningObjectMembersSettings)
                                                 .ToDictionary(x => x.Name);

            var copyingObjectMembersSettings = new VariableInfoDescriptor()
            {
                Flags             = flags,
                IncludeIfReadable = true,
            };

            var copyingObjectMembersByNameList = typeof(SourceType)
                                                 .GetVariableMembers(descriptor: copyingObjectMembersSettings)
                                                 .ToDictionary(x => x.Name);

            var clonedObejct = (TargetType)declaredType.InstantiateUninitializedObject() !;

            foreach (var nameAndCloningObjectMembersPair in cloningObjectMembersByNameList)
            {
                var cloningObjectMembersKey = nameAndCloningObjectMembersPair.Key;

                if (copyingObjectMembersByNameList.ContainsKey(cloningObjectMembersKey))
                {
                    var cloningObjectMember = nameAndCloningObjectMembersPair.Value;
                    var copyingObjectMember = copyingObjectMembersByNameList[cloningObjectMembersKey];

                    if (!cloningObjectMember.GetVariableType().IsAssignableFrom(copyingObjectMember.GetVariableType()))
                    {
                        continue;
                    }

                    var copyingObjectVariableValue = copyingObjectMember.GetValue(source);
                    cloningObjectMember.SetValue(clonedObejct, copyingObjectVariableValue);
                }
            }

            return(clonedObejct);
        }
        public static JsonSerializerSettings GetSettings(this JsonSerializer serializer)
        {
            var jsonSerializerSettingsVariableInfoSettings = new VariableInfoDescriptor()
            {
                IncludeIfWritable = true,
            };

            var serializerSettingsVariableInfoByNameList = typeof(JsonSerializerSettings)
                                                           .GetVariableMembers(descriptor: jsonSerializerSettingsVariableInfoSettings)
                                                           .ToDictionary(x => x.Name);

            var jsonSerializerVariableInfoSettings = new VariableInfoDescriptor()
            {
                IncludeIfReadable = true,
            };

            var serializerVariableInfoByNameList = typeof(JsonSerializer)
                                                   .GetVariableMembers(descriptor: jsonSerializerVariableInfoSettings)
                                                   .ToDictionary(x => x.Name);

            var serializerSettings = new JsonSerializerSettings();

            foreach (var nameAndSerializerSettingsVariableInfoPair in serializerSettingsVariableInfoByNameList)
            {
                var serializerSettingsVariableInfoKey = nameAndSerializerSettingsVariableInfoPair.Key;

                if (serializerVariableInfoByNameList.ContainsKey(serializerSettingsVariableInfoKey))
                {
                    var serializerSettingsVariableInfo = nameAndSerializerSettingsVariableInfoPair.Value;
                    var serializerVariableInfo         = serializerVariableInfoByNameList[serializerSettingsVariableInfoKey];
                    var serializerVariableValue        = serializerVariableInfo.GetValue(serializer);

                    serializerSettingsVariableInfo.SetValue(serializerSettings, serializerVariableValue);
                }
            }

            return(serializerSettings);
        }
 public static bool IsVariable(this MemberInfo memberInfo, VariableInfoDescriptor descriptor, Type attributeType, bool getCustomAttributesInherit)
 => IsVariable(memberInfo, descriptor) && isVariable(memberInfo, attributeType, getCustomAttributesInherit);