/// <summary>
        /// Initializes a new instance of the <see cref="DataSerializerWithDataContracts"/> class.
        /// </summary>
        /// <param name="knownTypes">The known types.</param>
        public DataSerializerWithDataContracts(ICollection <Type> knownTypes)
        {
            if (knownTypes.Count == 0)
            {
                throw new InvalidOperationException(
                          "DataContractMessageSerializer requires some known types to serialize. Have you forgot to supply them?");
            }

            _knownTypes = knownTypes;

            ThrowOnMessagesWithoutDataContracts(_knownTypes);

            foreach (var type in _knownTypes)
            {
                var reference = ContractEvil.GetContractReference(type);
                try
                {
                    _contract2Type.Add(reference, type);
                }
                catch (ArgumentException ex)
                {
                    var format = String.Format("Failed to add contract reference '{0}'", reference);
                    throw new InvalidOperationException(format, ex);
                }

                _type2Contract.Add(type, reference);
            }
        }
Example #2
0
        public DataSerializerWithProtoBuf(ICollection <Type> knownTypes)
        {
            if (knownTypes.Count == 0)
            {
                throw new InvalidOperationException(
                          "ProtoBuf requires some known types to serialize. Have you forgot to supply them?");
            }
            foreach (var type in knownTypes)
            {
                var reference = ContractEvil.GetContractReference(type);
                var formatter = RuntimeTypeModel.Default.CreateFormatter(type);

                try
                {
                    _contract2Type.Add(reference, type);
                }
                catch (ArgumentException e)
                {
                    var msg = string.Format("Duplicate contract '{0}' being added to ProtoBuf dictionary", reference);
                    throw new InvalidOperationException(msg, e);
                }
                try
                {
                    _type2Contract.Add(type, reference);
                    _type2Formatter.Add(type, formatter);
                }
                catch (ArgumentException e)
                {
                    var msg = string.Format("Duplicate type '{0}' being added to ProtoBuf dictionary", type);
                    throw new InvalidOperationException(msg, e);
                }
            }
        }
Example #3
0
        public void Class_can_override()
        {
            var contractReference = ContractEvil.GetContractReference(typeof(CustomDataClass));

            Assert.AreEqual("Custom/ProtoBufDataTests/Type", contractReference);
        }
Example #4
0
        public void Default_reference_is_type_name()
        {
            var contractReference = ContractEvil.GetContractReference(typeof(SimpleDataClass));

            Assert.AreEqual("ProtoBufDataTests/SimpleDataClass", contractReference);
        }
        public void Class_can_override()
        {
            var contractReference = ContractEvil.GetContractReference(typeof(CustomProtoClass));

            Assert.AreEqual("ProtoBufNativeTests/Custom", contractReference);
        }