Пример #1
0
        public void TestGetValue()
        {
            DummyClass c = new DummyClass {
                b1 = true
            };

            Assert.Equal(true, wrapper.GetValue(c));
        }
Пример #2
0
        public void TestGetValue__Private()
        {
            PropertyInfo    property = typeof(DummyClass).GetProperty(nameof(DummyClass.b2));
            PropertyWrapper wrapper2 = new PropertyWrapper(property);
            DummyClass      c        = new DummyClass {
                b2 = true
            };

            Assert.Equal(true, wrapper2.GetValue(c));
        }
Пример #3
0
        /// <summary>
        /// copy property value from source to target. Deep cloning used if necessary
        /// </summary>
        private static void CopyPropertyValue(object source, object target, PropertyWrapper propertyWrapper, Dictionary <object, object> clonedObjects)
        {
            var value = propertyWrapper.GetValue(source);

            if (value == null)
            {
                return;
            }

            if (propertyWrapper.CloningMode == CloningMode.Deep)
            {
                value = Clone(propertyWrapper.TypeWrapper, value, clonedObjects);
            }

            propertyWrapper.SetValue(target, value);
        }
Пример #4
0
        private void FillObject(TypeWrapper wrapper, object target, IDataAdapter data)
        {
            if (wrapper.IsSelfSerializable)
            {
                ((INanoSerializable)target).Deserialize(data, this);
                return;
            }

            // read properties
            for (int i = 0; i < wrapper.Properties.Count; i++)
            {
                PropertyWrapper property = wrapper.Properties[i];

                // should be already loaded at this time
                if (property.ConstructorArg != -1 && property.State != NanoState.SerializeSet)
                {
                    continue;
                }

                if (property.Info.GetMethod.IsPrivate && property.Info.SetMethod.IsPrivate &&
                    (flags & OptimizationFlags.PrivateProperties) == 0)
                {
                    continue;
                }

                if (property.TypeCategory == TypeCategory.Primitive ||
                    property.TypeCategory == TypeCategory.Enum)
                {
                    if (!property.Info.CanWrite)
                    {
                        continue;
                    }

                    property.SetValue(
                        target,
                        DeserializePrimitive(
                            property.MemberType,
                            data,
                            property.Name,
                            property.Location != NanoLocation.SubNode,
                            (flags & OptimizationFlags.EnumAsValue) != 0
                            )
                        );
                }
                else
                {
                    IDataAdapter subnode = data.GetChild(property.Name);
                    if (subnode != null)
                    {
                        object currentValue = property.GetValue(target);
                        if (currentValue == null)
                        {
                            property.SetValue(target, DeserializeObject(property.MemberType, subnode, null));
                        }
                        else
                        {
                            DeserializeObject(property.MemberType, subnode, currentValue);
                        }
                    }
                }
            }

            // read fields
            for (int i = 0; i < wrapper.Fields.Count; i++)
            {
                FieldWrapper field = wrapper.Fields[i];

                // should be already loaded at this time
                if (field.ConstructorArg != -1 && field.State != NanoState.SerializeSet)
                {
                    continue;
                }

                if (field.TypeCategory == TypeCategory.Primitive ||
                    field.TypeCategory == TypeCategory.Enum)
                {
                    field.SetValue(
                        target,
                        DeserializePrimitive(
                            field.MemberType,
                            data,
                            field.Name,
                            field.Location != NanoLocation.SubNode,
                            (flags & OptimizationFlags.EnumAsValue) != 0
                            )
                        );
                }
                else
                {
                    IDataAdapter subnode = data.GetChild(field.Name);
                    if (subnode != null)
                    {
                        object value = DeserializeObject(field.MemberType, subnode, null);
                        if (value != null)
                        {
                            field.SetValue(target, value);
                        }
                    }
                }
            }
        }
Пример #5
0
        private void SerializeValue(IDataAdapter data, object target)
        {
            objectsCache.Add(target, maxObjId++);
            TypeWrapper wrapper = TypeCache.GetWrapper(target.GetType());

            if (wrapper.TrySerialize(target, data, this))
            {
                return;
            }

            for (int i = 0; i < wrapper.Properties.Count; i++)
            {
                PropertyWrapper property = wrapper.Properties[i];

                if (property.IsPrivate)
                {
                    if (!settings.SerializePrivateProperties)
                    {
                        continue;
                    }

                    havePrivateProperties = true;
                }

                bool isReadOnly;
                if (property.ConstructorArg != -1 || // will be used in constructor
                    settings.SerializeReadOnly)
                {
                    isReadOnly = false; // always serialize
                }
                else
                {
                    isReadOnly = !property.CanWrite;
                }

                object value = property.GetValue(target);
                if (value != null)
                {
                    SerializeValue(property.MemberType, value, data, new Info(property), isReadOnly);
                }
                else if (settings.SerializeNull)
                {
                    data.AddNullValue(property.Name, property.Location != NanoLocation.SubNode);
                }
            }

            for (int i = 0; i < wrapper.Fields.Count; i++)
            {
                FieldWrapper field = wrapper.Fields[i];

                if (!settings.IgnoreNotSerialized && field.Info.IsNotSerialized)
                {
                    continue;
                }

                object value = field.GetValue(target);
                if (value != null)
                {
                    SerializeValue(field.MemberType, value, data, new Info(field), false);
                }
                else if (settings.SerializeNull)
                {
                    data.AddNullValue(field.Name, field.Location != NanoLocation.SubNode);
                }
            }
        }