/// <summary> /// Sets supplemental stream metadata. /// </summary> /// <typeparam name="T">Type of supplemental metadata.</typeparam> /// <param name="value">Supplemental metadata value.</param> /// <param name="serializers">Known serializers.</param> internal void SetSupplementalMetadata <T>(T value, KnownSerializers serializers) { this.SupplementalMetadataTypeName = typeof(T).AssemblyQualifiedName; var handler = serializers.GetHandler <T>(); var writer = new BufferWriter(this.supplementalMetadataBytes); handler.Serialize(writer, value, new SerializationContext(serializers)); this.supplementalMetadataBytes = writer.Buffer; }
public TypeSchema Initialize(KnownSerializers serializers, TypeSchema targetSchema) { this.handler = serializers.GetHandler <SharedContainer <T> >(); var type = this.GetType(); var name = TypeSchema.GetContractName(type, serializers.RuntimeVersion); var innerMember = new TypeMemberSchema("inner", typeof(SharedContainer <T>).AssemblyQualifiedName, true); var schema = new TypeSchema(name, TypeSchema.GetId(name), type.AssemblyQualifiedName, TypeFlags.IsClass, new TypeMemberSchema[] { innerMember }, Version); return(targetSchema ?? schema); }
public TypeSchema Initialize(KnownSerializers serializers, TypeSchema targetSchema) { serializers.GetHandler <byte>(); // register element type var type = typeof(byte[]); var name = TypeSchema.GetContractName(type, serializers.RuntimeVersion); var elementsMember = new TypeMemberSchema("Elements", typeof(byte).AssemblyQualifiedName, true); var schema = new TypeSchema(name, TypeSchema.GetId(name), type.AssemblyQualifiedName, TypeFlags.IsCollection, new TypeMemberSchema[] { elementsMember }, Version); return(targetSchema ?? schema); }
/// <summary> /// Gets supplemental stream metadata. /// </summary> /// <typeparam name="T">Type of supplemental metadata.</typeparam> /// <param name="serializers">Known serializers.</param> /// <returns>Supplemental metadata.</returns> internal T GetSupplementalMetadata <T>(KnownSerializers serializers) { if (string.IsNullOrEmpty(this.SupplementalMetadataTypeName)) { throw new InvalidOperationException("Stream does not contain supplemental metadata."); } if (typeof(T) != Type.GetType(this.SupplementalMetadataTypeName)) { throw new InvalidCastException($"Supplemental metadata type mismatch ({this.SupplementalMetadataTypeName})."); } var handler = serializers.GetHandler <T>(); var reader = new BufferReader(this.supplementalMetadataBytes); var target = default(T); handler.Deserialize(reader, ref target, new SerializationContext(serializers)); return(target); }
public void RegisterFlags() { var serializers = new KnownSerializers(); // register different flags for multiple types serializers.Register <ClassWithIntPtr>(CloningFlags.CloneIntPtrFields); serializers.Register <ClassWithPointer>(CloningFlags.ClonePointerFields); serializers.Register <ClassWithNonSerialized>(CloningFlags.SkipNonSerializedFields); serializers.Register <StructWithAll>( CloningFlags.ClonePointerFields | CloningFlags.CloneIntPtrFields | CloningFlags.SkipNonSerializedFields); Assert.AreEqual(CloningFlags.CloneIntPtrFields, serializers.GetCloningFlags(typeof(ClassWithIntPtr))); Assert.AreEqual(CloningFlags.ClonePointerFields, serializers.GetCloningFlags(typeof(ClassWithPointer))); Assert.AreEqual(CloningFlags.SkipNonSerializedFields, serializers.GetCloningFlags(typeof(ClassWithNonSerialized))); Assert.AreEqual( CloningFlags.ClonePointerFields | CloningFlags.CloneIntPtrFields | CloningFlags.SkipNonSerializedFields, serializers.GetCloningFlags(typeof(StructWithAll))); serializers = new KnownSerializers(); // new context // registering with no flags is equivalent to None serializers.Register <ClassWithPointer>(); serializers.Register <ClassWithPointer>(CloningFlags.None); Assert.AreEqual(CloningFlags.None, serializers.GetCloningFlags(typeof(ClassWithPointer))); serializers = new KnownSerializers(); // new context // cannot re-register with different flags serializers.Register <ClassWithPointer>(CloningFlags.ClonePointerFields); Assert.ThrowsException <SerializationException>( () => serializers.Register <ClassWithPointer>(CloningFlags.SkipNonSerializedFields)); Assert.AreEqual(CloningFlags.ClonePointerFields, serializers.GetCloningFlags(typeof(ClassWithPointer))); serializers = new KnownSerializers(); // new context // once a handler has been created, cannot register flags var handler = serializers.GetHandler <ClassWithPointer>(); Assert.ThrowsException <SerializationException>( () => serializers.Register <ClassWithPointer>(CloningFlags.ClonePointerFields)); }
/// <summary> /// Initializes a new instance of the <see cref="SerializerComponent{T}"/> class. /// </summary> /// <param name="pipeline">Pipeline to which this component belongs.</param> /// <param name="serializers">Known serializers.</param> internal SerializerComponent(Pipeline pipeline, KnownSerializers serializers) : base(pipeline) { this.context = new SerializationContext(serializers); this.handler = serializers.GetHandler <T>(); }
/// <summary> /// Initializes a new instance of the <see cref="SerializerComponent{T}"/> class. /// </summary> /// <param name="pipeline">The pipeline to add the component to.</param> /// <param name="serializers">Known serializers.</param> /// <param name="name">An optional name for the component.</param> internal SerializerComponent(Pipeline pipeline, KnownSerializers serializers, string name = nameof(SerializerComponent <T>)) : base(pipeline, name) { this.context = new SerializationContext(serializers); this.handler = serializers.GetHandler <T>(); }