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

loresoft / EntityChange / 13417967502

19 Feb 2025 04:51PM UTC coverage: 45.43% (+5.7%) from 39.697%
13417967502

push

github

pwelter34
fix failing test

287 of 726 branches covered (39.53%)

Branch coverage included in aggregate %.

563 of 1145 relevant lines covered (49.17%)

73.33 hits per line

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

0.0
/src/EntityChange/Reflection/FieldAccessor.cs
1
#nullable disable
2

3
using System.Reflection;
4

5
namespace EntityChange.Reflection;
6

7
/// <summary>
8
/// An accessor class for <see cref="FieldInfo"/>.
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.
17
    /// </summary>
18
    /// <param name="fieldInfo">The <see cref="FieldInfo"/> instance to use for this accessor.</param>
19
    public FieldAccessor(FieldInfo fieldInfo) : base(fieldInfo)
×
20
    {
21
        if (fieldInfo == null)
×
22
            throw new ArgumentNullException(nameof(fieldInfo));
×
23

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

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

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

34
        HasSetter = !isReadonly;
×
35
    }
×
36

37
    /// <summary>
38
    /// Gets the type of the member.
39
    /// </summary>
40
    /// <value>The type of the member.</value>
41
    public override Type MemberType { get; }
×
42

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

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

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

61

62
    /// <summary>
63
    /// Returns the value of the member.
64
    /// </summary>
65
    /// <param name="instance">The object whose member value will be returned.</param>
66
    /// <returns>
67
    /// The member value for the instance parameter.
68
    /// </returns>
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 member.
86
    /// </summary>
87
    /// <param name="instance">The object whose member value will be set.</param>
88
    /// <param name="value">The new value for this member.</param>
89
    public override void SetValue(object instance, object value)
90
    {
91
        if (instance == null)
×
92
            throw new ArgumentNullException(nameof(instance));
×
93

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

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

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