/
ComponentBase.cs
92 lines (81 loc) · 2.51 KB
/
ComponentBase.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
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
namespace Stride.Core
{
/// <summary>
/// Base class for a framework component.
/// </summary>
[DataContract]
public abstract class ComponentBase : DisposeBase, IComponent, ICollectorHolder
{
private string name;
private ObjectCollector collector;
/// <summary>
/// Initializes a new instance of the <see cref="ComponentBase"/> class.
/// </summary>
protected ComponentBase()
: this(null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ComponentBase"/> class.
/// </summary>
/// <param name="name">The name attached to this component</param>
protected ComponentBase(string name)
{
collector = new ObjectCollector();
Tags = new PropertyContainer(this);
Name = name ?? GetType().Name;
}
/// <summary>
/// Gets the attached properties to this component.
/// </summary>
[DataMemberIgnore] // Do not try to recreate object (preserve Tags.Owner)
public PropertyContainer Tags;
/// <summary>
/// Gets or sets the name of this component.
/// </summary>
/// <value>
/// The name.
/// </value>
[DataMemberIgnore] // By default don't store it, unless derived class are overriding this member
public virtual string Name
{
get
{
return name;
}
set
{
if (value == name) return;
name = value;
OnNameChanged();
}
}
/// <summary>
/// Disposes of object resources.
/// </summary>
protected override void Destroy()
{
collector.Dispose();
}
ObjectCollector ICollectorHolder.Collector
{
get
{
collector.EnsureValid();
return collector;
}
}
/// <summary>
/// Called when <see cref="Name"/> property was changed.
/// </summary>
protected virtual void OnNameChanged()
{
}
public override string ToString()
{
return $"{GetType().Name}: {name}";
}
}
}