-
Notifications
You must be signed in to change notification settings - Fork 39
/
Observable.cs
94 lines (82 loc) · 2.51 KB
/
Observable.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//---------------------------------------------------------------------------------
namespace ThroughputTest
{
using System;
public class Observable<T>
{
public class ChangingEventArgs : EventArgs
{
public T OldValue { get; private set; }
public T NewValue { get; private set; }
public bool Cancel { get; set; }
public ChangingEventArgs(T oldValue, T newValue)
{
OldValue = oldValue;
NewValue = newValue;
Cancel = false;
}
}
public class ChangedEventArgs : EventArgs
{
public T Value { get; private set; }
public ChangedEventArgs(T value)
{
Value = value;
}
}
public event EventHandler<ChangingEventArgs> Changing;
public event EventHandler<ChangedEventArgs> Changed;
protected T value;
public T Value
{
get
{
return value;
}
set
{
if (this.value.Equals(value))
{
return;
}
ChangingEventArgs e = new ChangingEventArgs(this.value, value);
OnChanging(e);
if (e.Cancel)
{
return;
}
this.value = value;
OnChanged(new ChangedEventArgs(this.value));
}
}
public Observable() { }
public Observable(T value)
{
this.value = value;
}
protected virtual void OnChanging(ChangingEventArgs e)
{
EventHandler<ChangingEventArgs> handler = Changing;
if (handler == null)
{
return;
}
handler(this, e);
}
protected virtual void OnChanged(ChangedEventArgs e)
{
EventHandler<ChangedEventArgs> handler = Changed;
if (handler == null)
{
return;
}
handler(this, e);
}
}
}