internal static IEnumerable <T> Expand <T>(byte[] data, string entityName, Func <T> objectBuilder) where T : PropertyBase { using (MemoryStream stream = new MemoryStream(data)) { while (stream.Position < stream.Length) { long startPosition = stream.Position; int propertyId = (int)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(int)); SqlPropertyDefinition propertyDefinition = MessageTraceCollapsedProperty.GetCollapsedPropertyById(entityName, propertyId); T property = objectBuilder(); property.PropertyName = propertyDefinition.PropertyName; switch (propertyDefinition.Type) { case SqlPropertyTypes.Int: property.PropertyValueInteger = new int?((int)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(int))); break; case SqlPropertyTypes.String: property.PropertyValueString = (string)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(string)); break; case SqlPropertyTypes.DateTime: property.PropertyValueDatetime = new DateTime?((DateTime)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(DateTime))); break; case SqlPropertyTypes.Decimal: property.PropertyValueDecimal = new decimal?((decimal)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(decimal))); break; case SqlPropertyTypes.Blob: property.PropertyValueBlob = new BlobType((string)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(string))); break; case SqlPropertyTypes.Boolean: property.PropertyValueBit = new bool?((bool)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(bool))); break; case SqlPropertyTypes.Guid: property.PropertyValueGuid = (Guid)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(Guid)); break; case SqlPropertyTypes.Long: property.PropertyValueLong = new long?((long)MessageTraceCollapsedProperty.ReadPrimitiveValueFromStream(stream, typeof(long))); break; default: throw new InvalidOperationException(string.Format("Property type {0} is not supported for MessageTraceCollapsedProperty.", propertyDefinition.Type)); } yield return(property); if (stream.Position <= startPosition) { throw new InvalidOperationException("Unable to expand. Stream position is not moving forward."); } } } yield break; }
static MessageTraceCollapsedProperty() { Dictionary <Type, Func <object, byte[]> > dictionary = new Dictionary <Type, Func <object, byte[]> >(); dictionary.Add(typeof(bool), (object value) => BitConverter.GetBytes((bool)value)); dictionary.Add(typeof(char), (object value) => BitConverter.GetBytes((char)value)); dictionary.Add(typeof(uint), (object value) => BitConverter.GetBytes((uint)value)); dictionary.Add(typeof(short), (object value) => BitConverter.GetBytes((short)value)); dictionary.Add(typeof(ulong), (object value) => BitConverter.GetBytes((ulong)value)); dictionary.Add(typeof(float), (object value) => BitConverter.GetBytes((float)value)); dictionary.Add(typeof(ushort), (object value) => BitConverter.GetBytes((ushort)value)); dictionary.Add(typeof(double), (object value) => BitConverter.GetBytes((double)value)); MessageTraceCollapsedProperty.PrimitiveValueToByteArrayConvertor = dictionary; Dictionary <Type, Func <byte[], object> > dictionary2 = new Dictionary <Type, Func <byte[], object> >(); dictionary2.Add(typeof(bool), (byte[] bytes) => BitConverter.ToBoolean(bytes, 0)); dictionary2.Add(typeof(char), (byte[] bytes) => BitConverter.ToChar(bytes, 0)); dictionary2.Add(typeof(uint), (byte[] bytes) => BitConverter.ToUInt32(bytes, 0)); dictionary2.Add(typeof(short), (byte[] bytes) => BitConverter.ToInt16(bytes, 0)); dictionary2.Add(typeof(ulong), (byte[] bytes) => BitConverter.ToUInt64(bytes, 0)); dictionary2.Add(typeof(float), (byte[] bytes) => BitConverter.ToSingle(bytes, 0)); dictionary2.Add(typeof(ushort), (byte[] bytes) => BitConverter.ToUInt16(bytes, 0)); dictionary2.Add(typeof(double), (byte[] bytes) => BitConverter.ToDouble(bytes, 0)); MessageTraceCollapsedProperty.PrimitiveValueFromByteArrayConvertor = dictionary2; SqlPropertyDefinition[] source = new SqlPropertyDefinition[] { new SqlPropertyDefinition { EntityName = "MessageEvents", EntityId = 3, PropertyName = "SFSLong", PropertyId = 1343, Type = SqlPropertyTypes.Long } }; MessageTraceCollapsedProperty.propertyDefinitionByName = source.ToLookup((SqlPropertyDefinition p) => p.PropertyName.ToLower()); MessageTraceCollapsedProperty.propertyDefinitionById = source.ToLookup((SqlPropertyDefinition p) => p.PropertyId); }
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); }