1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
7
8
using System;
9
10
using System.Runtime.Serialization;
11
using System.ComponentModel;
12
13
namespace SharpLib.Ear
14
{
15
16
/// <summary>
17
/// EAR object provides serialization support.
18
/// It assumes most derived class is decorated with AttributeObject.
19
/// </summary>
20
[DataContract]
21
[KnownType("DerivedTypes")]
22
public abstract class Object: IComparable, INotifyPropertyChanged
23
24
private bool iConstructed = false;
25
26
protected Object()
27
28
Construct();
29
}
30
31
32
/// Needed as our constructor is not called following internalization.
33
34
public void Construct()
35
36
if (!iConstructed)
37
38
DoConstruct();
39
iConstructed = true;
40
41
42
43
44
///
45
46
protected virtual void DoConstruct()
47
48
49
50
51
52
public enum State
53
54
Rest=0,
55
Edit
56
57
58
State iCurrentState = State.Rest;
59
public State CurrentState { get { return iCurrentState; } set { OnStateLeave(); iCurrentState = value; OnStateEnter(); } }
60
61
62
63
64
protected virtual void OnStateLeave()
65
66
67
68
69
70
71
72
protected virtual void OnStateEnter()
73
74
75
76
77
78
/// Static object name.
79
80
public string Name
81
82
//Get the name of this object attribute
83
get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
84
private set { }
85
86
87
88
/// Static object description.
89
90
public string Description
91
92
//Get the description of this object attribute
93
get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
94
95
96
97
98
/// Dynamic object description.
99
100
/// <returns></returns>
101
public virtual string Brief()
102
103
return Name;
104
105
106
107
/// Needed to make sure our sorting makes sense
108
109
/// <param name="obj"></param>
110
111
public int CompareTo(object obj)
112
113
//Sort by object name
114
return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
115
116
117
118
/// Tells whether the current object configuration is valid.
119
120
121
public virtual bool IsValid()
122
123
return true;
124
125
126
127
/// So that data contract knows all our types.
128
129
130
private static IEnumerable<Type> DerivedTypes()
131
132
return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
133
134
135
136
137
138
public event PropertyChangedEventHandler PropertyChanged;
139
140
141
142
/// Invoke our event.
143
144
/// <param name="name"></param>
145
protected void OnPropertyChanged(string name)
146
147
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
148
149
150
151
152