コード例 #1
0
        public static T Map <T>(this Dictionary <string, AttributeValue> values) where T : Base, new()
        {
            var entity = new T();

            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                var propertyType = property.PropertyType;

                if (values.ContainsKey(property.Name))
                {
                    if (AttributeValueConverter.ConvertToValue.ContainsKey(propertyType) && propertyType != typeof(object))
                    {
                        property.SetValue(entity, AttributeValueConverter.ConvertToValue[propertyType](values[property.Name]));
                    }
                    else if (propertyType.IsArray || (propertyType.IsGenericType && propertyType.GetInterfaces().Contains(typeof(IEnumerable))))
                    {
                        var declaredType = propertyType.GetDeclaringType();

                        if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                        {
                            var dictionary = AttributeValueConverter.ConvertToDictionary(propertyType, values[property.Name].M);

                            property.SetValue(entity, dictionary);
                        }
                        else if (ListAttributeValueConverter.AllTypes.Contains(declaredType))
                        {
                            var value = AttributeValueConverter.ConvertToArrayValue(declaredType, values[property.Name]);

                            property.SetValue(entity, AttributeValueConverter.FromList(propertyType, value));
                        }
                        else if (declaredType.IsClass && !values[property.Name].NULL)
                        {
                            var value = values[property.Name].L;

                            property.SetValue(entity, AttributeValueConverter.FromList(propertyType, value));
                        }
                    }
                    else if (propertyType.IsClass)
                    {
                        var value = (Dictionary <string, AttributeValue>)AttributeValueConverter.ConvertToValue[typeof(object)](values[property.Name]);

                        property.SetValue(entity, AttributeValueConverter.FromDictionary(propertyType, value));
                    }
                }
            }

            return(entity);
        }
コード例 #2
0
        private static void MapFromAttributeValueToArray <T>(T entity, Dictionary <string, AttributeValue> values, Type propertyType, PropertyInfo property)
        {
            var declaredType = propertyType.GetDeclaringType();

            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                var dictionary = AttributeValueConverter.ConvertToDictionary(propertyType, values[property.Name].M);

                property.SetValue(entity, dictionary);
            }
            else if (ListAttributeValueConverter.AllTypes.Contains(declaredType))
            {
                var value = AttributeValueConverter.ConvertToArrayValue(declaredType, values[property.Name]);
                value = value.Any() ? value : null;
                property.SetValue(entity, AttributeValueConverter.FromList(propertyType, value));
            }
            else if (declaredType.IsClass && !values[property.Name].NULL)
            {
                var value = values[property.Name].L;

                property.SetValue(entity, AttributeValueConverter.FromList(propertyType, value));
            }
        }