Field.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Reflection;
  3. namespace ShwasherSys.ReflectionMagic
  4. {
  5. /// <summary>
  6. /// Provides a mechanism to access fields through the <see cref="IProperty"/> abstraction.
  7. /// </summary>
  8. internal class Field : IProperty
  9. {
  10. private readonly FieldInfo _fieldInfo;
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="Field"/> class wrapping the specified field.
  13. /// </summary>
  14. /// <param name="field">The field info to wrap.</param>
  15. /// <exception cref="ArgumentNullException">Thrown when <paramref name="field"/> is <c>null</c>.</exception>
  16. internal Field(FieldInfo field)
  17. {
  18. if (field == null)
  19. throw new ArgumentNullException(nameof(field));
  20. _fieldInfo = field;
  21. }
  22. public Type PropertyType => _fieldInfo.FieldType;
  23. string IProperty.Name => _fieldInfo.Name;
  24. object IProperty.GetValue(object obj, object[] index)
  25. {
  26. return _fieldInfo.GetValue(obj);
  27. }
  28. void IProperty.SetValue(object obj, object value, object[] index)
  29. {
  30. _fieldInfo.SetValue(obj, value);
  31. }
  32. }
  33. [Obsolete("Will be made internal in a future release.")]
  34. public static class FieldInfoExtensions
  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 FieldInfo info)
  38. {
  39. return new Field(info);
  40. }
  41. }
  42. }