Ejemplo n.º 1
0
        public void ManualCompiler()
        {
            var nilModel = TypeModel.NullModel.Instance;

            Assert.False(nilModel.IsDefined(typeof(string)));
            Assert.True(nilModel.CanSerialize(typeof(string)));
            Assert.True(nilModel.CanSerializeBasicType(typeof(string)));
            Assert.False(nilModel.CanSerializeContractType(typeof(string)));

            AssemblyName an = new AssemblyName {
                Name = "ManualCompiler"
            };
            AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly(an,
                                                                                AssemblyBuilderAccess.RunAndSave);
            ModuleBuilder module   = asm.DefineDynamicModule("ManualCompiler", "ManualCompiler.dll");
            var           baseType = typeof(TypeModel);
            var           type     = module.DefineType("MyModel", baseType.Attributes & ~TypeAttributes.Abstract, baseType);

            var t = type.CreateType();

            asm.Save("ManualCompiler.dll");

            TypeModel tm = (TypeModel)Activator.CreateInstance(t);

            Assert.False(tm.IsDefined(typeof(string)));
            Assert.True(tm.CanSerialize(typeof(string)));
            Assert.True(tm.CanSerializeBasicType(typeof(string)));
            Assert.False(tm.CanSerializeContractType(typeof(string)));
        }
Ejemplo n.º 2
0
        CacheItem ITranscoder.Serialize(object o)
        {
            if (o == null)
            {
                return(inner.Serialize(o));
            }
            Type type = o.GetType();

            if (model.CanSerializeContractType(type))
            {
                using (var ms = new MemoryStream())
                {
                    WriteType(ms, type);
                    Interlocked.Increment(ref serializedCount);
                    model.Serialize(ms, o);
                    return(new CacheItem(ProtoIdentifier, new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length)));
                }
            }
            else
            {
                return(inner.Serialize(o));
            }
        }
Ejemplo n.º 3
0
        static void TestEnumModel(TypeModel model)
        {
            var obj = model.GetSerializer <Foo>();

            Assert.NotNull(obj);

            Assert.True(obj.Features.GetCategory() == SerializerFeatures.CategoryScalar, "should be a scalar serializer; is " + obj.GetType().NormalizeName());

            using var ms = new MemoryStream();
            Assert.False(model.CanSerializeContractType(typeof(Foo)), "should not be a contract type");
            Assert.True(model.CanSerializeBasicType(typeof(Foo)), "should be a basic type");
            model.Serialize(ms, Foo.B);

            var hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length);

            Assert.Equal("08-01", hex);
            ms.Position = 0;
            var val = model.Deserialize <Foo>(ms);

            Assert.Equal(Foo.B, val);

            val = model.DeepClone(Foo.B);
            Assert.Equal(Foo.B, val);
        }
 /// <inheritdoc/>
 protected override bool CanReadType(Type type)
 => _model.CanSerializeContractType(type);