ModuleSetting.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Reflection;
  2. using Quadarax.Foundation.Core.Data.Interface.Entity;
  3. using Quadarax.Foundation.Core.Object.Extensions;
  4. using Quadarax.Foundation.Core.Reflection.Extensions;
  5. namespace Quadarax.Application.QLiberace.Common.Settings
  6. {
  7. public abstract class ModuleSetting : IDto
  8. {
  9. public void SetData(IEnumerable<Tuple<string, string, object?>> data)
  10. {
  11. if (data == null)
  12. throw new ArgumentNullException(nameof(data));
  13. var properties = this.GetType().GetAllProperties();
  14. foreach (var item in data)
  15. {
  16. var property = properties.FirstOrDefault(x => string.Equals(x.Name, item.Item1));
  17. if (property == null)
  18. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.SettingsHasNoPropertyCode,
  19. item.Item1, this.GetType().Name);
  20. property.SetValue(this, item.Item3);
  21. }
  22. }
  23. public IEnumerable<Tuple<string, string, object?>> GetData()
  24. {
  25. var properties = this.GetType().GetAllProperties();
  26. var result = this.GetAllPropertyValues().
  27. Select(x=> new Tuple<string, string, object?>(x.Key, properties.First(y=>y.Name == x.Key).GetFinalPropertyType().FullName ?? string.Empty, properties.First(y=>y.Name == x.Key).GetValue(this))).ToArray();
  28. return result;
  29. }
  30. }
  31. }