コード例 #1
0
        /// <summary>
        /// 序列化数据到 info 中。
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        protected override void Serialize(SerializationInfo info, StreamingContext context)
        {
            base.Serialize(info, context);

            //只序列化非默认值的编译期属性, 不序列化运行时属性
            foreach (var field in this._compiledFields)
            {
                if (field.HasValue)
                {
                    var property = field.Property;

                    var meta = property.GetMeta(this);
                    if (meta.Serializable)
                    {
                        var value = field.Value;
                        if (!object.Equals(value, meta.DefaultValue))
                        {
                            info.AddValue(property.Name, value, property.PropertyType);
                        }
                    }
                }
            }

            //同时,还需要序列化未标记 NonSerialized 的字段。
            var clrFields = FieldsSerializationHelper.EnumerateSerializableFields(info.ObjectType);

            foreach (var f in clrFields)
            {
                var v     = f.GetValue(this);
                var vType = v != null?v.GetType() : f.FieldType;

                info.AddValue(f.Name, v, vType);
            }
        }
コード例 #2
0
        protected ManagedPropertyObject(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            var compiledProperties = this.PropertiesContainer.GetNonReadOnlyCompiledProperties();

            this.InitFields(compiledProperties);

            var clrFields = FieldsSerializationHelper.EnumerateSerializableFields(info.ObjectType);

            //遍历所有已经序列化的属性值序列
            var compiledPropertiesCount = compiledProperties.Count;
            var allValues = info.GetEnumerator();

            while (allValues.MoveNext())
            {
                var serializationEntry = allValues.Current;
                var name = serializationEntry.Name;

                bool isManagedProperty = false;

                //找到对应的属性赋值。
                for (int i = 0; i < compiledPropertiesCount; i++)
                {
                    var property = compiledProperties[i];
                    if (property.Name == name)
                    {
                        this._LoadProperty(property, serializationEntry.Value);
                        isManagedProperty = true;
                        break;
                    }
                }

                //如果没有打到对应的托管属性设置,则尝试找到私有字段进行设置。
                if (!isManagedProperty)
                {
                    var f = FieldsSerializationHelper.FindSingleField(clrFields, name);
                    if (f != null)
                    {
                        var value = TypeHelper.CoerceValue(f.FieldType, serializationEntry.Value);
                        f.SetValue(this, value);
                    }
                }
            }
        }