private static bool SavePrimitiveValueToStream(Stream stream, Type typeOfvalue, object value)
 {
     if (typeOfvalue == typeof(long))
     {
         MessageTraceCollapsedProperty.SaveByteArrayToStream(stream, BitConverter.GetBytes((long)value));
         return(true);
     }
     if (typeOfvalue == typeof(int))
     {
         MessageTraceCollapsedProperty.SaveByteArrayToStream(stream, BitConverter.GetBytes((int)value));
         return(true);
     }
     if (typeOfvalue == typeof(Guid))
     {
         MessageTraceCollapsedProperty.SaveByteArrayToStream(stream, ((Guid)value).ToByteArray());
         return(true);
     }
     if (typeOfvalue == typeof(string))
     {
         string text = value as string;
         if (Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(text)) == text)
         {
             MessageTraceCollapsedProperty.SaveSingleByteToStream(stream, 2);
             MessageTraceCollapsedProperty.SaveByteArrayToStream(stream, Encoding.ASCII.GetBytes((string)value));
         }
         else
         {
             MessageTraceCollapsedProperty.SaveSingleByteToStream(stream, 3);
             MessageTraceCollapsedProperty.SaveByteArrayToStream(stream, Encoding.Unicode.GetBytes((string)value));
         }
         return(true);
     }
     if (typeOfvalue == typeof(DateTime))
     {
         return(MessageTraceCollapsedProperty.SavePrimitiveValueToStream(stream, typeof(long), ((DateTime)value).ToBinary()));
     }
     if (MessageTraceCollapsedProperty.PrimitiveValueToByteArrayConvertor.ContainsKey(typeOfvalue))
     {
         MessageTraceCollapsedProperty.SaveByteArrayToStream(stream, MessageTraceCollapsedProperty.PrimitiveValueToByteArrayConvertor[typeOfvalue](value));
         return(true);
     }
     return(typeOfvalue.IsEnum && MessageTraceCollapsedProperty.SavePrimitiveValueToStream(stream, typeof(int), Convert.ToInt32(value)));
 }
        internal static byte[] Collapse(byte[] existingData, string entityName, PropertyBase property)
        {
            byte[] result;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                if (existingData != null && existingData.Length > 0)
                {
                    memoryStream.Write(existingData, 0, existingData.Length);
                }
                SqlPropertyDefinition collapsedPropertyByName = MessageTraceCollapsedProperty.GetCollapsedPropertyByName(entityName, property.PropertyName);
                switch (collapsedPropertyByName.Type)
                {
                case SqlPropertyTypes.Int:
                    if (property.PropertyValueInteger != null)
                    {
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), collapsedPropertyByName.PropertyId);
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), property.PropertyValueInteger.Value);
                    }
                    break;

                case SqlPropertyTypes.String:
                    if (!string.IsNullOrEmpty(property.PropertyValueString))
                    {
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), collapsedPropertyByName.PropertyId);
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(string), property.PropertyValueString);
                    }
                    break;

                case SqlPropertyTypes.DateTime:
                    if (property.PropertyValueDatetime != null)
                    {
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), collapsedPropertyByName.PropertyId);
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(DateTime), property.PropertyValueDatetime.Value);
                    }
                    break;

                case SqlPropertyTypes.Decimal:
                    if (property.PropertyValueDecimal != null)
                    {
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), collapsedPropertyByName.PropertyId);
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(decimal), property.PropertyValueDecimal.Value);
                    }
                    break;

                case SqlPropertyTypes.Blob:
                    if (!string.IsNullOrEmpty(property.PropertyValueBlob.Value))
                    {
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), collapsedPropertyByName.PropertyId);
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(string), property.PropertyValueBlob.Value);
                    }
                    break;

                case SqlPropertyTypes.Boolean:
                    if (property.PropertyValueBit != null)
                    {
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), collapsedPropertyByName.PropertyId);
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(bool), property.PropertyValueBit.Value);
                    }
                    break;

                case SqlPropertyTypes.Guid:
                    if (property.PropertyValueGuid != Guid.Empty)
                    {
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), collapsedPropertyByName.PropertyId);
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(Guid), property.PropertyValueGuid);
                    }
                    break;

                case SqlPropertyTypes.Long:
                    if (property.PropertyValueLong != null)
                    {
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(int), collapsedPropertyByName.PropertyId);
                        MessageTraceCollapsedProperty.SavePrimitiveValueToStream(memoryStream, typeof(long), property.PropertyValueLong.Value);
                    }
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Property type {0} is not supported for MessageTraceCollapsedProperty.", collapsedPropertyByName.Type));
                }
                result = memoryStream.ToArray();
            }
            return(result);
        }