-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Field.cs
61 lines (50 loc) · 1.83 KB
/
Field.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.ComponentModel;
using System.Reflection;
using FluentAssertions.Common;
namespace FluentAssertions.Equivalency
{
/// <summary>
/// A specialized type of <see cref="INode "/> that represents a field of an object in a structural equivalency assertion.
/// </summary>
public class Field : Node, IMember
{
private readonly FieldInfo fieldInfo;
private bool? isBrowsable;
public Field(FieldInfo fieldInfo, INode parent)
: this(fieldInfo.ReflectedType, fieldInfo, parent)
{
}
public Field(Type reflectedType, FieldInfo fieldInfo, INode parent)
{
this.fieldInfo = fieldInfo;
DeclaringType = fieldInfo.DeclaringType;
ReflectedType = reflectedType;
Path = parent.PathAndName;
GetSubjectId = parent.GetSubjectId;
Name = fieldInfo.Name;
Type = fieldInfo.FieldType;
RootIsCollection = parent.RootIsCollection;
}
public Type ReflectedType { get; }
public object GetValue(object obj)
{
return fieldInfo.GetValue(obj);
}
public Type DeclaringType { get; set; }
public override string Description => $"field {GetSubjectId().Combine(PathAndName)}";
public CSharpAccessModifier GetterAccessibility => fieldInfo.GetCSharpAccessModifier();
public CSharpAccessModifier SetterAccessibility => fieldInfo.GetCSharpAccessModifier();
public bool IsBrowsable
{
get
{
if (isBrowsable == null)
{
isBrowsable = fieldInfo.GetCustomAttribute<EditorBrowsableAttribute>() is not { State: EditorBrowsableState.Never };
}
return isBrowsable.Value;
}
}
}
}