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

loresoft / FluentCommand / 16300818838

15 Jul 2025 06:03PM UTC coverage: 54.951% (+0.3%) from 54.61%
16300818838

push

github

pwelter34
import data tweaks, xml doc updates

1716 of 3630 branches covered (47.27%)

Branch coverage included in aggregate %.

78 of 143 new or added lines in 11 files covered. (54.55%)

7 existing lines in 5 files now uncovered.

4361 of 7429 relevant lines covered (58.7%)

231.0 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>
UNCOV
20
    public FieldAccessor(FieldInfo fieldInfo) : base(fieldInfo)
×
21
    {
22
        if (fieldInfo == null)
×
23
            throw new ArgumentNullException(nameof(fieldInfo));
×
24

25
        Name = fieldInfo.Name;
×
26
        MemberType = fieldInfo.FieldType;
×
27

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

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

35
        HasSetter = !isReadonly;
×
36
    }
×
37

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

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

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

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

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

74
        if (_getter == null || !HasGetter)
×
75
            throw new InvalidOperationException($"Field '{Name}' does not have a getter.");
×
76

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

81
        return get(instance);
×
82
    }
83

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

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

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

103
        set(instance, value);
×
104
    }
×
105
}
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