FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tug/src/Tug.Base/Util/ReadOnlyConfiguration.cs at master · PowerShellOrg/tug · GitHub
PowerShellOrg
/
tug
Public
Notifications
You must be signed in to change notification settings
Fork
29
Star
164
Code
Issues
26
Pull requests
1
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
tug
/
src
/
Tug.Base
/
Util
/
ReadOnlyConfiguration.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (73 loc) · 2.77 KB
Breadcrumbs
tug
/
src
/
Tug.Base
/
Util
/
ReadOnlyConfiguration.cs
Copy path
File metadata and controls
84 lines (73 loc) · 2.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// PowerShell.org Tug DSC Pull Server
// Copyright (c) The DevOps Collective, Inc. All rights reserved.
// Licensed under the MIT license. See the LICENSE file in the project root for more information.
using
System
;
using
System
.
Collections
.
Generic
;
using
Microsoft
.
Extensions
.
Configuration
;
using
Microsoft
.
Extensions
.
Primitives
;
namespace
Tug
.
Util
{
/// <summary>
/// A <see cref="IConfiguration">configuration</see> implementation
// that wraps another instance and prevents modifications to the wrapped instance.
/// </summary>
/// <remarks>
/// When wrapping another instance, you can optionally indicate whether write
/// attempts generate exceptions or are silently ignored.
/// <p>
/// Upon first access, nested configuration elements are themselves wrapped in read-only
/// implementations before being returned.
/// </p>
/// </remarks>
public
class
ReadOnlyConfiguration
:
IConfiguration
{
private
IConfiguration
_inner
;
private
bool
_throwOnWrite
;
private
Dictionary
<
string
,
ReadOnlyConfigurationSection
>
_children
;
/// <param name="throwOnWrite"><c>true</c> by default which indicates
/// exceptions will be thrown for any write attempts</param>
public
ReadOnlyConfiguration
(
IConfiguration
inner
,
bool
throwOnWrite
=
true
)
{
if
(
inner
==
null
)
throw
new
ArgumentNullException
(
nameof
(
inner
)
)
;
_inner
=
inner
;
_throwOnWrite
=
throwOnWrite
;
}
public
string
this
[
string
key
]
{
get
{
return
_inner
[
key
]
;
}
set
{
if
(
_throwOnWrite
)
{
throw
new
InvalidOperationException
(
/*SR*/
"Attempt to write a read-only configuration"
)
;
}
}
}
public
IConfigurationSection
GetSection
(
string
key
)
{
var
cs
=
_inner
.
GetSection
(
key
)
;
return
cs
==
null
?
null
:
GetReadOnlySection
(
key
)
;
}
public
IEnumerable
<
IConfigurationSection
>
GetChildren
(
)
{
foreach
(
var
cs
in
_inner
.
GetChildren
(
)
)
{
yield
return
GetReadOnlySection
(
cs
.
Key
)
;
}
}
public
IChangeToken
GetReloadToken
(
)
{
return
_inner
.
GetReloadToken
(
)
;
}
private
ReadOnlyConfigurationSection
GetReadOnlySection
(
string
key
)
{
if
(
_children
==
null
)
_children
=
new
Dictionary
<
string
,
ReadOnlyConfigurationSection
>
(
)
;
if
(
!
_children
.
ContainsKey
(
key
)
)
_children
.
Add
(
key
,
new
ReadOnlyConfigurationSection
(
_inner
.
GetSection
(
key
)
)
)
;
return
_children
[
key
]
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL