/// <summary>
        /// Deserialize a user type instance from a POF stream by reading its
        /// state using the specified <see cref="IPofReader"/> object.
        /// </summary>
        /// <remarks>
        /// An implementation of <b>IPofSerializer</b> is required to follow
        /// the following steps in sequence for reading in an object of a
        /// user type:
        /// <list type="number">
        /// <item>
        /// <description>
        /// If the object is evolvable, the implementation must get the
        /// version by calling <see cref="IPofWriter.VersionId"/>.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// The implementation may read any combination of the
        /// properties of the user type by using "read" methods of the
        /// <b>IPofReader</b>, but it must do so in the order of the property
        /// indexes.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// After all desired properties of the user type have been read,
        /// the implementation must terminate the reading of the user type by
        /// calling <see cref="IPofReader.ReadRemainder"/>.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <param name="reader">
        /// The <b>IPofReader</b> with which to read the object's state.
        /// </param>
        /// <returns>
        /// The deserialized user type instance.
        /// </returns>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public virtual object Deserialize(IPofReader reader)
        {
            ITypeMetadata <object> tmd = m_tmd;
            object value = tmd.NewInstance();

            // set the version identifier
            bool       isEvolvable = value is IEvolvable;
            IEvolvable evolvable   = null;

            if (isEvolvable)
            {
                evolvable             = (IEvolvable)value;
                evolvable.DataVersion = reader.VersionId;
            }

            // POF Annotation processing
            for (IEnumerator enmr = tmd.GetAttributes(); enmr.MoveNext();)
            {
                IAttributeMetadata <object> attr = (IAttributeMetadata <object>)enmr.Current;
                attr.Set(value, attr.Codec.Decode(reader, attr.Index));
            }

            // read any future properties
            Binary remainder = reader.ReadRemainder();

            if (isEvolvable)
            {
                evolvable.FutureData = remainder;
            }

            return(value);
        }
Exemplo n.º 2
0
        public void TestClassMetadata()
        {
            MethodInfo method = typeof(ClassMetadataDescribable).GetMethod("GetName");

            var builder = new ClassMetadataBuilder <ClassMetadataDescribable>();
            ITypeMetadata <ClassMetadataDescribable> tmd = builder.SetClass(typeof(ClassMetadataDescribable))
                                                           .SetHash("TestDomain".GetHashCode())
                                                           .SetTypeId(1234)
                                                           .AddAttribute(builder.NewAttribute()
                                                                         .SetIndex(0)
                                                                         .SetName("name")
                                                                         .SetCodec(Codecs.DEFAULT_CODEC)
                                                                         .SetInvocationStrategy(new InvocationStrategies.MethodInvcationStrategy <ClassMetadataDescribable>(method))
                                                                         .SetVersion(0).Build()).Build();

            ITypeMetadata <ClassMetadataDescribable> tmd2 = builder.SetClass(typeof(ClassMetadataDescribable))
                                                            .SetHash("TestDomain".GetHashCode())
                                                            .SetTypeId(1234)
                                                            .AddAttribute(builder.NewAttribute()
                                                                          .SetIndex(0)
                                                                          .SetName("name")
                                                                          .SetCodec(Codecs.DEFAULT_CODEC)
                                                                          .SetInvocationStrategy(new InvocationStrategies.MethodInvcationStrategy <ClassMetadataDescribable>(method))
                                                                          .SetVersion(0).Build()).Build();

            Assert.AreEqual(1234, tmd.GetKey().GetTypeId());
            Assert.IsTrue(tmd.GetKey().GetHash() != 0);
            Assert.IsNotNull(tmd.GetAttribute("name"));
            Assert.AreEqual("name", tmd.GetAttribute("name").GetName());
            Assert.AreEqual(0, tmd.GetAttribute("name").GetIndex());
            Assert.AreEqual(0, tmd.GetAttribute("name").GetVersion());
            Assert.AreEqual(tmd, tmd2);
            Assert.AreEqual(tmd.GetHashCode(), tmd2.GetHashCode());

            ClassMetadataDescribable cmdd = new ClassMetadataDescribable("augusta");

            Assert.AreEqual("augusta", tmd.GetAttribute("name").Get(cmdd));
            tmd.GetAttribute("name").Set(cmdd, "ada");
            Assert.AreEqual("ada", tmd.GetAttribute("name").Get(cmdd));
            Assert.IsInstanceOf(typeof(Codecs.DefaultCodec), tmd.GetAttribute("name").GetCodec());
            Assert.IsTrue(tmd.GetAttributes().MoveNext());
            Assert.IsInstanceOf(typeof(ClassMetadataDescribable), tmd.NewInstance());

            object a = (object)"dasd";
        }