Tanoda
FieldCacheEntry.cs
Go to the documentation of this file.
1using System;
2using System.Reflection;
3
5{
7 {
8 public FieldCacheEntry(object ins, FieldInfo f) : this(ins, f, null) { }
9 public FieldCacheEntry(object ins, FieldInfo f, ICacheEntry parent) : base(GetMemberName(ins, f))
10 {
11 if (f == null)
12 throw new ArgumentNullException();
13
14 _instance = ins;
15 FieldInfo = f;
16 _parent = parent;
17 }
18
19 internal static string GetMemberName(object ins, MemberInfo f)
20 {
21 if (ins != null)
22 if (f != null)
23 return f.Name;
24 if (f != null) return "S/" + f.Name;
25 return "S/";
26 }
27
28 public FieldInfo FieldInfo { get; private set; }
29 private readonly object _instance;
30 private readonly ICacheEntry _parent;
31
32 public override object GetValueToCache()
33 {
34 return FieldInfo.GetValue(_instance);
35 }
36
37 protected override bool OnSetValue(object newValue)
38 {
39 if (!FieldInfo.IsInitOnly)
40 {
41 FieldInfo.SetValue(_instance, newValue);
42 // Needed for structs to propagate changes back to the original field/prop
43 if (_parent != null && _parent.CanSetValue()) _parent.SetValue(_instance);
44 return true;
45 }
46 return false;
47 }
48
49 public override Type Type()
50 {
51 return FieldInfo.FieldType;
52 }
53
54 public override bool CanSetValue()
55 {
56 return (FieldInfo.Attributes & FieldAttributes.Literal) == 0 && (_parent == null || _parent.CanSetValue());
57 }
58 }
59}
FieldInfo FieldInfo
override Type Type()
override bool CanSetValue()
override object GetValueToCache()
FieldCacheEntry(object ins, FieldInfo f, ICacheEntry parent)
override bool OnSetValue(object newValue)
FieldCacheEntry(object ins, FieldInfo f)
Definition: ICacheEntry.cs:6
string Name()