コード例 #1
0
        public void WritePolymorphicObject(CborDiscriminatorPolicy discriminatorPolicy, string hexBuffer)
        {
            CborOptions options = new CborOptions();

            options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention <string>(options.Registry));
            options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject));
            options.Registry.ObjectMappingRegistry.Register <NameObject>(om =>
            {
                om.AutoMap();
                om.SetDiscriminatorPolicy(discriminatorPolicy);
            });
            options.Registry.ObjectMappingRegistry.Register <BaseObject>(om =>
            {
                om.AutoMap();
                om.SetDiscriminatorPolicy(discriminatorPolicy);
            });

            BaseObjectHolder obj = new BaseObjectHolder
            {
                BaseObject = new NameObject
                {
                    Id   = 1,
                    Name = "foo"
                },
                NameObject = new NameObject
                {
                    Id   = 2,
                    Name = "bar"
                }
            };

            Helper.TestWrite(obj, hexBuffer, null, options);
        }
コード例 #2
0
        public void ReadPolymorphicObject()
        {
            CborOptions options = new CborOptions();

            options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention <string>(options.Registry));
            options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject));

            const string     hexBuffer = "A16A426173654F626A656374A3625F746A4E616D654F626A656374644E616D6563666F6F62496401";
            BaseObjectHolder obj       = Helper.Read <BaseObjectHolder>(hexBuffer, options);

            Assert.NotNull(obj);
            Assert.IsType <NameObject>(obj.BaseObject);
            Assert.Equal("foo", ((NameObject)obj.BaseObject).Name);
            Assert.Equal(1, obj.BaseObject.Id);
        }
コード例 #3
0
        public void ReadWithCustomDiscriminator()
        {
            CborOptions options = new CborOptions();

            options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new CustomDiscriminatorConvention());
            options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject));

            const string     hexBuffer = "A16A426173654F626A656374A364747970651A22134C83644E616D6563666F6F62496401";
            BaseObjectHolder obj       = Helper.Read <BaseObjectHolder>(hexBuffer, options);

            Assert.NotNull(obj);
            Assert.IsType <NameObject>(obj.BaseObject);
            Assert.Equal("foo", ((NameObject)obj.BaseObject).Name);
            Assert.Equal(1, obj.BaseObject.Id);
        }
コード例 #4
0
        public void WriteWithCustomDiscriminator()
        {
            CborOptions options = new CborOptions();

            options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new CustomDiscriminatorConvention());
            options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject));

            const string hexBuffer = "A26A426173654F626A656374A364747970651A22134C83644E616D6563666F6F624964016A4E616D654F626A656374F6";

            BaseObjectHolder obj = new BaseObjectHolder
            {
                BaseObject = new NameObject
                {
                    Id   = 1,
                    Name = "foo"
                },
            };

            Helper.TestWrite(obj, hexBuffer, null, options);
        }