Exemplo n.º 1
0
 public void TestGetEnum()
 {
     Assert.AreEqual(TestEnum.Value1, StringValueAttribute.Get <TestEnum>("Test1"));
     Assert.AreEqual(TestEnum.Value2, StringValueAttribute.Get <TestEnum>(""));
     Assert.AreEqual(TestEnum.Value3, StringValueAttribute.Get <TestEnum>(null));
     Assert.AreEqual(TestEnum.Value1, StringValueAttribute.Get <TestEnum>("Missing")); // default(0) is Value1
 }
Exemplo n.º 2
0
 public void TestGetString()
 {
     Assert.AreEqual("Test1", StringValueAttribute.Get(TestEnum.Value1));
     Assert.AreEqual("", StringValueAttribute.Get(TestEnum.Value2));
     Assert.AreEqual(null, StringValueAttribute.Get(TestEnum.Value3));
     Assert.AreEqual(null, StringValueAttribute.Get(TestEnum.Value4));
 }
Exemplo n.º 3
0
        public void TestStringValueAttributeGet()
        {
            TestClass2 item  = new TestClass2(7, "test", 0);
            string     value = StringValueAttribute.Get(() => item.Id);

            Assert.IsNotNull(value);
            Assert.IsTrue(((string)value).StartsWith("IdTest"));
        }
 public override byte[] Serialize()
 {
     using (var stream = new MemoryStream())
     {
         stream.WriteASCII(StringValueAttribute.Get(MessageType));
         stream.WriteASCII(' ');
         stream.WriteASCII(FileId.ToString());
         stream.WriteASCII("\r\n\r\n");
         return(stream.ToArray());
     }
 }
Exemplo n.º 5
0
 public override byte[] Serialize()
 {
     using (var stream = new MemoryStream())
     {
         stream.WriteASCII(StringValueAttribute.Get(MessageType));
         stream.WriteASCII(' ');
         stream.WriteASCII(VersionM.ToString("D"));
         stream.WriteASCII('.');
         stream.WriteASCII(VersionN.ToString("D"));
         stream.WriteASCII(' ');
         stream.WriteASCII(FileId.ToString());
         stream.WriteASCII(' ');
         stream.WriteASCII(ChunkNo.ToString("D"));
         stream.WriteASCII(' ');
         stream.WriteASCII(ReplicationDeg.ToString("D"));
         stream.WriteASCII("\r\n\r\n");
         stream.Write(Body);
         return(stream.ToArray());
     }
 }
Exemplo n.º 6
0
        public static Message Deserialize(byte[] data)
        {
            if (data == null)
            {
                return(null);
            }

            var    minLength = Math.Min(15, data.Length); // assuming max type length is 15
            string typeStr   = null;

            for (var i = 0; i < minLength; ++i)
            {
                if (data[i] == ' ')
                {
                    typeStr = Encoding.ASCII.GetString(data, 0, i);
                    break;
                }
            }

            if (string.IsNullOrWhiteSpace(typeStr))
            {
                return(null);
            }

            var type = StringValueAttribute.Get <MessageType>(typeStr);

            if (type == MessageType.None)
            {
                return(null);
            }

            Func <byte[], Message> deserializeMethod;

            if (_deserializeDict.TryGetValue(type, out deserializeMethod))
            {
                return(deserializeMethod(data));
            }
            return(null);
        }