Property.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Reflection;
  3. namespace ShwasherSys.ReflectionMagic
  4. {
  5. /// <summary>
  6. /// Provides an mechanism to access properties through the <see cref="IProperty"/> abstraction.
  7. /// </summary>
  8. internal class Property : IProperty
  9. {
  10. private readonly PropertyInfo _propertyInfo;
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="Property"/> class wrapping the specified property.
  13. /// </summary>
  14. /// <param name="property">The property info to wrap.</param>
  15. /// <exception cref="ArgumentNullException">Thrown when <paramref name="property"/> is <c>null</c>.</exception>
  16. internal Property(PropertyInfo property)
  17. {
  18. if (property == null)
  19. throw new ArgumentNullException(nameof(property));
  20. _propertyInfo = property;
  21. }
  22. public Type PropertyType => _propertyInfo.PropertyType;
  23. string IProperty.Name => _propertyInfo.Name;
  24. object IProperty.GetValue(object obj, object[] index)
  25. {
  26. return _propertyInfo.GetValue(obj, index);
  27. }
  28. void IProperty.SetValue(object obj, object value, object[] index)
  29. {
  30. _propertyInfo.SetValue(obj, value, index);
  31. }
  32. }
  33. [Obsolete("Will be made internal in a future release.")]
  34. public static class PropertyInfoExtensions
  35. {
  36. [Obsolete("This is an internal API. If you are using this consider opening an issue on GitHub.")]
  37. public static IProperty ToIProperty(this PropertyInfo info)
  38. {
  39. return new Property(info);
  40. }
  41. }
  42. }