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
        private static bool IsKnownType(TypeModel model, Type type, out bool isList)
        {
            if (model != null && type != null)
            {
                if (model.CanSerialize(type, true, true, true, out var category))
                {
                    isList = category.IsRepeated();
                    return(true);
                }
            }

            isList = false;
            return(false);
        }
        public override byte[] ToBinary(object obj)
        {
            // NotificationMessage

            var notificationMessage = obj as NotificationMessage;

            if (notificationMessage != null)
            {
                using (var ms = new MemoryStream())
                {
                    // write code, observerId and notificationId

                    ms.WriteByte((byte)MessageCode.Notification);
                    ms.Write7BitEncodedInt(notificationMessage.ObserverId);
                    ms.Write7BitEncodedInt(notificationMessage.NotificationId);

                    // write message

                    WriteType(ms, notificationMessage.InvokePayload.GetType());
                    try
                    {
                        AkkaSurrogate.CurrentSystem = system;
                        _typeModel.Serialize(ms, notificationMessage.InvokePayload);
                    }
                    finally
                    {
                        AkkaSurrogate.CurrentSystem = null;
                    }

                    return(ms.ToArray());
                }
            }

            // RequestMessage

            var requestMessage = obj as RequestMessage;

            if (requestMessage != null)
            {
                using (var ms = new MemoryStream())
                {
                    // write code & requestId

                    ms.WriteByte((byte)MessageCode.Request);
                    ms.Write7BitEncodedInt(requestMessage.RequestId);

                    // write message

                    WriteType(ms, requestMessage.InvokePayload.GetType());
                    try
                    {
                        AkkaSurrogate.CurrentSystem = system;
                        _typeModel.Serialize(ms, requestMessage.InvokePayload);
                    }
                    finally
                    {
                        AkkaSurrogate.CurrentSystem = null;
                    }

                    return(ms.ToArray());
                }
            }

            // ResponseMessage

            var responseMessage = obj as ResponseMessage;

            if (responseMessage != null)
            {
                using (var ms = new MemoryStream())
                {
                    if (responseMessage.Exception == null && responseMessage.ReturnPayload == null)
                    {
                        ms.WriteByte((byte)MessageCode.ReplyWithNothing);
                        ms.Write7BitEncodedInt(responseMessage.RequestId);
                    }
                    else if (responseMessage.Exception != null)
                    {
                        ms.WriteByte((byte)MessageCode.ReplyWithException);
                        ms.Write7BitEncodedInt(responseMessage.RequestId);

                        var exceptionType = responseMessage.Exception.GetType();
                        WriteType(ms, exceptionType);
                        if (_typeModel.CanSerialize(exceptionType))
                        {
                            _typeModel.Serialize(ms, responseMessage.Exception);
                        }
                    }
                    else
                    {
                        ms.WriteByte((byte)MessageCode.ReplyWithResult);
                        ms.Write7BitEncodedInt(responseMessage.RequestId);

                        // write result

                        WriteType(ms, responseMessage.ReturnPayload.GetType());
                        try
                        {
                            AkkaSurrogate.CurrentSystem = system;
                            _typeModel.Serialize(ms, responseMessage.ReturnPayload);
                        }
                        finally
                        {
                            AkkaSurrogate.CurrentSystem = null;
                        }
                    }
                    return(ms.ToArray());
                }
            }

            throw new InvalidOperationException(
                      "ProtobufSerializer supports only NotificationMessage, RequestMessage and ResponseMessage.");
        }
 protected override bool CanReadType(Type type)
 {
     return(_typeModel.CanSerialize(type));
 }
Ejemplo n.º 5
0
 public bool CanSerialize(Type type)
 {
     return(_model.CanSerialize(type));
 }