private object CheckNull(object payload, DataDescriptor descriptor) { if (payload == null) { if (descriptor.CompressionTypeId != CompressionTypeHelper.NumericTypeId) { return 0; } } return payload; }
private object CheckNull(object payload, DataDescriptor descriptor) { if (descriptor.CompressionTypeId != CompressionTypeHelper.NumericTypeId && payload != null) { long value; if (long.TryParse(payload.ToString(), out value) && value == 0) { return null; } } return payload; }
protected IDictionary<Type, PayloadDescriptor> BuildPayloadsCache() { // Getting all payloads that have a payload attribute var types = _locator.Value.GetAssemblies() .SelectMany(GetTypesSafe) .Where(HasPayloadAttribute); // Building cache entries for each descriptor // Each descriptor is stored in dictionary under a key // that is it's name var cacheEntries = types .Select(type => new PayloadDescriptor { Type = type, ID = Interlocked.Increment(ref _payloadDescriptorID), Settings = new CompressionSettings { RoundNumbersTo = ((PayloadAttribute)Attribute.GetCustomAttribute(type, typeof(PayloadAttribute))).RoundNumbersTo }, Data = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) //.Where(p => !p.PropertyType.IsGenericParameter) .Select(propertyInfo => { var descriptor = new DataDescriptor { Name = propertyInfo.Name, Type = propertyInfo.PropertyType, Enumerable = propertyInfo.PropertyType.IsEnumerable(), SetValue = (baseObject, newValue) => { propertyInfo.SetValue(baseObject, newValue, null); }, GetValue = (baseObject) => { if (type.IsGenericType) { // When the type is generic, we have to GetValue from the // constructed type, not the definition return baseObject.GetType() .GetProperty(propertyInfo.Name) .GetValue(baseObject, null); } // This is more perfomant when the type is not generic return propertyInfo.GetValue(baseObject, null); } }; descriptor.CompressionTypeId = CompressionTypeHelper.GetCompressionType(descriptor); return descriptor; }) .Union(type.GetFields(BindingFlags.Public | BindingFlags.Instance) .Select(fieldInfo => { var descriptor = new DataDescriptor { Name = fieldInfo.Name, Type = fieldInfo.FieldType, Enumerable = fieldInfo.FieldType.IsEnumerable(), SetValue = (baseObject, newValue) => { fieldInfo.SetValue(baseObject, newValue); }, GetValue = (baseObject) => { if (type.IsGenericType) { // When the type is generic, we have to GetValue from the // constructed type, not the definition return baseObject.GetType() .GetField(fieldInfo.Name) .GetValue(baseObject); } // This is more perfomant when the type is not generic return fieldInfo.GetValue(baseObject); } }; descriptor.CompressionTypeId = CompressionTypeHelper.GetCompressionType(descriptor); return descriptor; })) .OrderBy(dataDescriptor => dataDescriptor.Name) }) .ToDictionary(payload => payload.Type, payload => payload); return cacheEntries; }