-
Notifications
You must be signed in to change notification settings - Fork 7
/
Ref.cs
48 lines (42 loc) · 1.15 KB
/
Ref.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
using System;
using System.Reflection;
namespace ModLib
{
public class Ref
{
private readonly Func<object> getter;
private readonly Action<object> setter;
private readonly PropertyInfo propInfo = null;
private readonly object instance = null;
public object Value
{
get
{
if (propInfo != null)
return propInfo.GetValue(instance);
else
return getter();
}
set
{
if (propInfo != null)
propInfo.SetValue(instance, value);
else
setter(value);
}
}
public Ref(Func<object> getter, Action<object> setter)
{
this.getter = getter ?? throw new ArgumentNullException(nameof(getter));
this.setter = setter;
}
public Ref(Func<object> getter) : this(getter, null)
{
}
public Ref(PropertyInfo propInfo, object instance)
{
this.propInfo = propInfo;
this.instance = instance;
}
}
}