Пример #1
0
        private static string CreateHeader <T>()
        {
            List <PropertyInfo> propertyInfos = GetCSVAttributeProperties <T>();
            StringBuilder       sb            = new StringBuilder();

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                CSVAttributeAttribute csvAttribute = propertyInfo.GetCustomAttribute <CSVAttributeAttribute>();
                sb.Append(csvAttribute.Name);
                sb.Append(CSVFile.SEPARATOR);
            }
            sb.Remove(sb.Length - 1, 1);
            sb.AppendLine();
            return(sb.ToString());
        }
Пример #2
0
        private T CreateEntity <T, U>(CSVFile file, CSVRow row, Guid projectId)
            where T : BaseEntity
            where U : BaseDto
        {
            T entity = Activator.CreateInstance <T>();

            if (typeof(IProjectAwareEntity).IsAssignableFrom(typeof(T)))
            {
                ((IProjectAwareEntity)entity).ProjectId = projectId;
            }
            if (typeof(IStateAwareEntity).IsAssignableFrom(typeof(T)))
            {
                ((IStateAwareEntity)entity).State = State.BUILTIN;
            }

            List <PropertyInfo> csvAttributePropertyInfos = typeof(U).GetProperties().Where(x => Attribute.IsDefined(x, typeof(CSVAttributeAttribute))).ToList();

            foreach (PropertyInfo csvAttributePropertyInfo in csvAttributePropertyInfos)
            {
                CSVAttributeAttribute csvAttribute = csvAttributePropertyInfo.GetCustomAttribute <CSVAttributeAttribute>();
                CSVValue currentValue = file.GetValueToColumn(row, csvAttribute.Name);
                if (currentValue == null)
                {
                    ValidationResult validationResult = new ValidationResult();
                    validationResult.Add(new ValidationMessage(ValidationType.ERROR, MessageKeyConstants.ERROR_MESSAGE_WRONG_CSV_FILE_ON_INPUT));
                    throw new ValidationException(validationResult);
                }
                object convertedValue = Converter.ConvertValue(csvAttributePropertyInfo.PropertyType, currentValue.GetValue());

                PropertyInfo entityPropertyInfo = entity.GetType().GetProperty(csvAttributePropertyInfo.Name);
                if (entityPropertyInfo != null)
                {
                    entityPropertyInfo.SetValue(entity, convertedValue);
                }
            }
            return(entity);
        }