TypeMapping.cs 903 B

12345678910111213141516171819202122232425262728
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace FormulaEnginePoC.FormulaEngine.Mapping
  6. {
  7. internal class TypeMapping
  8. {
  9. private readonly PropertyMapping[] _props;
  10. private readonly Dictionary<string, PropertyMapping> _nameDictionary;
  11. public PropertyMapping[] Properties => _props.ToArray();
  12. public TypeMapping(Type dataType)
  13. {
  14. var propertyInfos = dataType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead).ToArray();
  15. _props = propertyInfos.Select(p => new PropertyMapping(p)).ToArray();
  16. _nameDictionary = _props.ToDictionary(p => p.Display);
  17. }
  18. public bool TryGetProperty(string display, out PropertyMapping mapping)
  19. {
  20. return _nameDictionary.TryGetValue(display, out mapping);
  21. }
  22. }
  23. }