/CsvFileReader/Models/Types/ListAttributeService.cs

https://bitbucket.org/14wrivera/csvfilereader · C# · 105 lines · 68 code · 7 blank · 30 comment · 11 complexity · ca14b9506308468dd46616e68d29072f MD5 · raw file

  1. namespace CsvFileReader.Models.Types
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.Reflection;
  6. using CsvFileReader.Decorators;
  7. using CsvFileReader.Exceptions;
  8. /// <summary>
  9. /// resolve List type values
  10. /// </summary>
  11. public class ListAttributeService
  12. {
  13. #region Private Static Members
  14. private static ListAttributeService instance;
  15. #endregion
  16. #region Public Static Methods
  17. /// <summary>
  18. /// Singleton Instance
  19. /// </summary>
  20. /// <returns></returns>
  21. public static ListAttributeService Retrieve()
  22. {
  23. return ListAttributeService.instance;
  24. }
  25. #endregion
  26. #region Constructor
  27. /// <summary>
  28. /// Singleton Constructor
  29. /// </summary>
  30. static ListAttributeService()
  31. {
  32. ListAttributeService.instance = new ListAttributeService();
  33. }
  34. /// <summary>
  35. /// Singleton class so a private constructor is needed
  36. /// </summary>
  37. private ListAttributeService() { }
  38. #endregion
  39. #region Public Methods
  40. /// <summary>
  41. /// resolve lists
  42. /// </summary>
  43. /// <param name="obj">This is the instantiated model that we are mapping properties to</param>
  44. /// <param name="info">is a <typeparamref name="PropertyInfo"/>. The object info</param>
  45. /// <param name="contents">is a <typeparamref name="String"/>. The Value</param>
  46. public void ResolveList(Object obj, PropertyInfo info, string contents)
  47. {
  48. CsvListProperty attribute = info.GetCustomAttributes(typeof(CsvListProperty), false).Cast<CsvListProperty>().SingleOrDefault();
  49. if (attribute != null)
  50. {
  51. if (attribute.Delimiter == '\0')
  52. throw new CsvPropertyDecoratorException(info.Name + ": Missing Delimiter. Lists need a defined Delimiter");
  53. //get type in list
  54. Type itemType = info.PropertyType.GetGenericArguments()[0];
  55. //instantiate the list
  56. Object list = Activator.CreateInstance(info.PropertyType.Assembly.FullName, info.PropertyType.FullName).Unwrap();
  57. //split contents by defined delimiter
  58. foreach (string item in contents.Split(attribute.Delimiter))
  59. {
  60. SetValueByType(info, attribute, itemType, list, item);
  61. }
  62. info.SetValue(obj, list, null);
  63. }
  64. else
  65. {
  66. throw new CsvPropertyDecoratorException(info.Name + ": Missing Attribute Decorator of CsvPropertyAttribute. Lists need a defined Delimiter");
  67. }
  68. }
  69. /// <summary>
  70. /// Sets the vale based on the type
  71. /// </summary>
  72. /// <param name="info">is a <typeparamref name="PropertyInfo"/>. The object info</param>
  73. /// <param name="attribute">is a <typeparamref name="CsvListProperty"/>. Attribute this property has</param>
  74. /// <param name="itemType">is a <typeparamref name="Type"/>. the type</param>
  75. /// <param name="list">the list to bind to</param>
  76. /// <param name="item">the value</param>
  77. private void SetValueByType(PropertyInfo info, CsvListProperty attribute, Type itemType, Object list, string item)
  78. {
  79. if (itemType.IsClass)
  80. {
  81. ObjectAttributeService.Retrieve().ResolveObjectList(itemType, attribute, list, info, item);
  82. }
  83. else if (itemType.Name.Equals("Int32"))
  84. {
  85. list.GetType().GetMethod("Add").Invoke(list, new object[] { int.Parse(item) });
  86. }
  87. else if (itemType.Name.Equals("String"))
  88. {
  89. list.GetType().GetMethod("Add").Invoke(list, new object[] { item });
  90. }
  91. else if (itemType.Name.Equals("DateTime"))
  92. {
  93. DateAttributeService.Retrieve().ResolveListDate(list, info, item);
  94. }
  95. }
  96. #endregion
  97. }
  98. }