• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 26594173245

28 May 2026 06:28PM UTC coverage: 55.553% (+0.7%) from 54.902%
26594173245

push

github

pwelter34
Move JSON support, add docs and examples

1358 of 3215 branches covered (42.24%)

Branch coverage included in aggregate %.

103 of 234 new or added lines in 9 files covered. (44.02%)

371 existing lines in 26 files now uncovered.

4389 of 7130 relevant lines covered (61.56%)

312.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/FluentCommand/Reflection/FieldAccessor.cs
1
using System.Reflection;
2

3
namespace FluentCommand.Reflection;
4

5
/// <summary>
6
/// Provides an accessor for <see cref="FieldInfo"/> members, enabling efficient get and set operations
7
/// on fields using compiled delegates. Inherits from <see cref="MemberAccessor"/> to provide
8
/// reflection-based access and metadata retrieval for fields.
9
/// </summary>
10
public class FieldAccessor : MemberAccessor
11
{
12
    private readonly Lazy<Func<object, object?>> _getter;
13
    private readonly Lazy<Action<object, object?>>? _setter;
14

15
    /// <summary>
16
    /// Initializes a new instance of the <see cref="FieldAccessor"/> class using the specified <see cref="FieldInfo"/>.
17
    /// </summary>
18
    /// <param name="fieldInfo">The <see cref="FieldInfo"/> instance to use for this accessor.</param>
19
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="fieldInfo"/> is <c>null</c>.</exception>
20
    public FieldAccessor(FieldInfo fieldInfo) : base(fieldInfo)
×
21
    {
22
        ArgumentNullException.ThrowIfNull(fieldInfo);
×
23

UNCOV
24
        Name = fieldInfo.Name;
×
25
        MemberType = fieldInfo.FieldType;
×
26

UNCOV
27
        _getter = new Lazy<Func<object, object?>>(() => ExpressionFactory.CreateGet(fieldInfo));
×
28
        HasGetter = true;
×
29

UNCOV
30
        bool isReadonly = fieldInfo.IsInitOnly || fieldInfo.IsLiteral;
×
31
        if (!isReadonly)
×
32
            _setter = new Lazy<Action<object, object?>>(() => ExpressionFactory.CreateSet(fieldInfo));
×
33

UNCOV
34
        HasSetter = !isReadonly;
×
35
    }
×
36

37
    /// <summary>
38
    /// Gets the <see cref="Type"/> of the field.
39
    /// </summary>
40
    /// <value>The <see cref="Type"/> representing the field's data type.</value>
41
    public override Type MemberType { get; }
42

43
    /// <summary>
44
    /// Gets the name of the field.
45
    /// </summary>
46
    /// <value>The name of the field.</value>
47
    public override string Name { get; }
48

49
    /// <summary>
50
    /// Gets a value indicating whether this field has a getter.
51
    /// </summary>
52
    /// <value><c>true</c> if this field has a getter; otherwise, <c>false</c>.</value>
53
    public override bool HasGetter { get; }
54

55
    /// <summary>
56
    /// Gets a value indicating whether this field has a setter.
57
    /// </summary>
58
    /// <value><c>true</c> if this field has a setter; otherwise, <c>false</c>.</value>
59
    public override bool HasSetter { get; }
60

61
    /// <summary>
62
    /// Returns the value of the field for the specified object instance.
63
    /// </summary>
64
    /// <param name="instance">The object whose field value will be returned.</param>
65
    /// <returns>The value of the field for the given <paramref name="instance"/>.</returns>
66
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="instance"/> is <c>null</c>.</exception>
67
    /// <exception cref="InvalidOperationException">Thrown if the field does not have a getter.</exception>
68
    public override object? GetValue(object instance)
69
    {
UNCOV
70
        ArgumentNullException.ThrowIfNull(instance);
×
71

72
        if (!HasGetter)
×
UNCOV
73
            throw new InvalidOperationException($"Field '{Name}' does not have a getter.");
×
74

75
        var get = _getter.Value;
×
UNCOV
76
        if (get == null)
×
77
            throw new InvalidOperationException($"Field '{Name}' does not have a getter.");
×
78

79
        return get(instance);
×
80
    }
81

82
    /// <summary>
83
    /// Sets the value of the field for the specified object instance.
84
    /// </summary>
85
    /// <param name="instance">The object whose field value will be set.</param>
86
    /// <param name="value">The new value for this field.</param>
87
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="instance"/> is <c>null</c>.</exception>
88
    /// <exception cref="InvalidOperationException">Thrown if the field does not have a setter.</exception>
89
    public override void SetValue(object instance, object? value)
90
    {
UNCOV
91
        ArgumentNullException.ThrowIfNull(instance);
×
92

93
        if (_setter == null || !HasSetter)
×
94
            throw new InvalidOperationException($"Field '{Name}' does not have a setter.");
×
95

96
        var set = _setter.Value;
×
97
        if (set == null)
×
UNCOV
98
            throw new InvalidOperationException($"Field '{Name}' does not have a setter.");
×
99

100
        set(instance, value);
×
101
    }
×
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc