Tanoda
PropertyCacheEntry.cs
Go to the documentation of this file.
1using System;
2using System.Reflection;
3
5{
7 {
8 public PropertyCacheEntry(object ins, PropertyInfo p) : this(ins, p, null) { }
9 public PropertyCacheEntry(object ins, PropertyInfo p, ICacheEntry parent) : base(FieldCacheEntry.GetMemberName(ins, p))
10 {
11 if (p == null)
12 throw new ArgumentNullException();
13
14 _instance = ins;
15 PropertyInfo = p;
16 _parent = parent;
17 }
18
19 public PropertyInfo PropertyInfo { get; private set; }
20 private readonly object _instance;
21 private readonly ICacheEntry _parent;
22
23 public override object GetValueToCache()
24 {
25 if (!PropertyInfo.CanRead)
26 return "WRITE ONLY";
27
28 try
29 {
30 return PropertyInfo.GetValue(_instance, null);
31 }
32 catch (TargetInvocationException ex)
33 {
34 return ex.InnerException ?? ex;
35 }
36 catch (Exception ex)
37 {
38 return ex;
39 }
40 }
41
42 protected override bool OnSetValue(object newValue)
43 {
44 if (PropertyInfo.CanWrite)
45 {
46 PropertyInfo.SetValue(_instance, newValue, null);
47 // Needed for structs to propagate changes back to the original field/prop
48 if (_parent != null && _parent.CanSetValue()) _parent.SetValue(_instance);
49 return true;
50 }
51 return false;
52 }
53
54 public override Type Type()
55 {
56 return PropertyInfo.PropertyType;
57 }
58
59 public override bool CanSetValue()
60 {
61 return PropertyInfo.CanWrite && (_parent == null || _parent.CanSetValue());
62 }
63 }
64}
PropertyCacheEntry(object ins, PropertyInfo p, ICacheEntry parent)
PropertyInfo PropertyInfo
override Type Type()
PropertyCacheEntry(object ins, PropertyInfo p)
override object GetValueToCache()
override bool CanSetValue()
override bool OnSetValue(object newValue)
Definition: ICacheEntry.cs:6