-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path7.more_class.ps1
82 lines (71 loc) · 1.87 KB
/
7.more_class.ps1
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
class BaseClass
{
BaseClass()
{
# Default ctor
}
hidden [void] _setProperties ([System.Collections.IDictionary]$Definition)
{
foreach ($key in $Definition.Keys)
{
if ($this.PSObject.Properties[$key].isSettable)
{
$this.($key) = $Definition[$key]
}
else
{
Write-Warning -Message ('Key {0} is not settable' -f $key)
}
}
}
hidden [void] _setProperties ([PSCustomObject] $Definition)
{
$Definition.PSObject.Properties.name.Foreach{
if ($this.PSObject.Properties[$_].isSettable)
{
$this.($_) = $Definition.($_)
}
else
{
Write-Warning -Message ('Key {0} is not settable' -f $_)
}
}
}
hidden static [object[]] Load([string] $fileName)
{
return (Get-Content -Raw -Path $fileName | ConvertFrom-Yaml -AllDocuments -UseMergingParser | ForEach-Object -Process {
[organiser] $_
})
}
}
class home : BaseClass
{
[string] $City
[string] $Zip
}
class organiser : BaseClass
{
[string] $Name
[int] $age
[home] $address
organiser ([string] $fileName)
{
$this._setProperties((Get-Content -Raw -Path $fileName | ConvertFrom-Yaml))
}
organiser ([PSCustomObject] $Definition)
{
$this._setProperties($Definition)
}
organiser ([System.Collections.IDictionary]$Definition)
{
$this._setProperties($Definition)
}
}
break
# creer l'objet depuis un objet/hashtable
$a = [organiser]([PSCustomObject]@{name='Willy';age=44;address=@{City='Geneve';zip=90210}})
$a
# charger l'objet depuis un yaml
$b = [organiser]'.\5.object_definition.yml'
# charger tout les objets depuis un yaml
[organiser]::Load('.\5.object_definition.yml')