private static object ToSagaData(Type sagaDataType, DictionaryTableEntity entity) { var toCreate = Activator.CreateInstance(sagaDataType); foreach (var accessor in GetPropertyAccessors(sagaDataType)) { if (!entity.ContainsKey(accessor.Name)) { continue; } var value = entity[accessor.Name]; var type = accessor.PropertyType; if (type == typeof(byte[])) { accessor.Setter(toCreate, value.BinaryValue); } else if (TrySetNullable(value, toCreate, accessor)) { } else if (type == typeof(string)) { accessor.Setter(toCreate, value.StringValue); } else { if (value.PropertyType == EdmType.String) { // possibly serialized JSON.NET value try { using (var stringReader = new StringReader(value.StringValue)) { var deserialized = jsonSerializer.Deserialize(stringReader, type); accessor.Setter(toCreate, deserialized); } } catch (Exception) { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage and it cannot be deserialized with JSON.NET."); } } else { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage"); } } } return(toCreate); }
public static object ToEntity(Type entityType, DictionaryTableEntity entity) { var toCreate = Activator.CreateInstance(entityType); foreach (var propertyInfo in entityType.GetProperties()) { if (entity.ContainsKey(propertyInfo.Name)) { var value = entity[propertyInfo.Name]; var type = propertyInfo.PropertyType; if (type == typeof(byte[])) { propertyInfo.SetValue(toCreate, value.BinaryValue, null); } else if (TrySetNullable(value, toCreate, propertyInfo)) { } else if (type == typeof(string)) { propertyInfo.SetValue(toCreate, value.StringValue, null); } else { if (value.PropertyType == EdmType.String) { // possibly serialized JSON.NET value try { using (var stringReader = new StringReader(value.StringValue)) { var deserialized = jsonSerializer.Deserialize(stringReader, type); propertyInfo.SetValue(toCreate, deserialized, null); } } catch (Exception) { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage and it cannot be deserialized with JSON.NET."); } } else { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage"); } } } } return toCreate; }