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

loresoft / EntityChange / 10792597827

10 Sep 2024 12:38PM UTC coverage: 39.608%. Remained the same
10792597827

push

github

web-flow
Merge pull request #103 from loresoft/dependabot/nuget/MinVer-6.0.0

Bump MinVer from 5.0.0 to 6.0.0

237 of 731 branches covered (32.42%)

Branch coverage included in aggregate %.

571 of 1309 relevant lines covered (43.62%)

60.49 hits per line

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

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

3
namespace EntityChange.Reflection;
4

5
/// <summary>
6
/// An accessor class for <see cref="FieldInfo"/>.
7
/// </summary>
8
public class FieldAccessor : MemberAccessor
9
{
10
    private readonly Lazy<Func<object, object>> _getter;
11
    private readonly Lazy<Action<object, object>> _setter;
12

13
    /// <summary>
14
    /// Initializes a new instance of the <see cref="FieldAccessor"/> class.
15
    /// </summary>
16
    /// <param name="fieldInfo">The <see cref="FieldInfo"/> instance to use for this accessor.</param>
17
    public FieldAccessor(FieldInfo fieldInfo) : base(fieldInfo)
×
18
    {
19
        if (fieldInfo == null)
×
20
            throw new ArgumentNullException(nameof(fieldInfo));
×
21

22
        Name = fieldInfo.Name;
×
23
        MemberType = fieldInfo.FieldType;
×
24

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

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

32
        HasSetter = !isReadonly;
×
33
    }
×
34

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

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

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

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

59

60
    /// <summary>
61
    /// Returns the value of the member.
62
    /// </summary>
63
    /// <param name="instance">The object whose member value will be returned.</param>
64
    /// <returns>
65
    /// The member value for the instance parameter.
66
    /// </returns>
67
    public override object GetValue(object instance)
68
    {
69
        if (instance == null)
×
70
            throw new ArgumentNullException(nameof(instance));
×
71

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

75
        var get = _getter.Value;
×
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 member.
84
    /// </summary>
85
    /// <param name="instance">The object whose member value will be set.</param>
86
    /// <param name="value">The new value for this member.</param>
87
    public override void SetValue(object instance, object value)
88
    {
89
        if (instance == null)
×
90
            throw new ArgumentNullException(nameof(instance));
×
91

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

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

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

© 2025 Coveralls, Inc