-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInjectedField.cs
52 lines (48 loc) · 2.34 KB
/
InjectedField.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
using System;
namespace Someta
{
/// <summary>
/// Represents a field that has been injected into the target class. Must be used in conjunction
/// with IStateExtensionPoint. To use, simply declare a property of this type and it will be
/// populated with an instance of this class that provides a proxy to allow getting and setting
/// values on the field.
/// </summary>
/// <typeparam name="T">The type of the value stored in the field.</typeparam>
public class InjectedField<T>
{
private readonly Func<object, T> getter;
private readonly Action<object, T> setter;
/// <summary>
/// When Someta finds an extension point that implements IStateExtensionPoint with properties of
/// type InjectedField those properties will be automatically assigned a new instance of this
/// class.
/// </summary>
/// <param name="getter">A delegate provided by Someta to get the value of the field.</param>
/// <param name="setter">A delegate provided by Someta to set the value of the field.</param>
public InjectedField(Func<object, T> getter, Action<object, T> setter)
{
this.getter = getter;
this.setter = setter;
}
/// <summary>
/// Call to get the current value of the field as stored on the provided instance.
/// </summary>
/// <param name="instance">The instance of the object the field is attached to. This must not be null.
/// To represent static fields, simply declare a static field on your extension point.</param>
/// <returns>The current value of the field.</returns>
public T GetValue(object instance)
{
return getter(instance);
}
/// <summary>
/// Call to set the current value of the field as stored on the provided instance.
/// </summary>
/// <param name="instance">The instance of the object the field is attached to. This must not be null.
/// To represent static fields, simply declare a static field on your extension point.</param>
/// <param name="value">The new value that the field should be set to.</param>
public void SetValue(object instance, T value)
{
setter(instance, value);
}
}
}