/
FeatureCollection.cs
119 lines (102 loc) · 3.28 KB
/
FeatureCollection.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Http.Features
{
public class FeatureCollection : IFeatureCollection
{
private static KeyComparer FeatureKeyComparer = new KeyComparer();
private readonly IFeatureCollection? _defaults;
private IDictionary<Type, object>? _features;
private volatile int _containerRevision;
public FeatureCollection()
{
}
public FeatureCollection(IFeatureCollection defaults)
{
_defaults = defaults;
}
public virtual int Revision
{
get { return _containerRevision + (_defaults?.Revision ?? 0); }
}
public bool IsReadOnly { get { return false; } }
public object? this[Type key]
{
get
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
object? result;
return _features != null && _features.TryGetValue(key, out result) ? result : _defaults?[key];
}
set
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
if (_features != null && _features.Remove(key))
{
_containerRevision++;
}
return;
}
if (_features == null)
{
_features = new Dictionary<Type, object>();
}
_features[key] = value;
_containerRevision++;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<Type, object>> GetEnumerator()
{
if (_features != null)
{
foreach (var pair in _features)
{
yield return pair;
}
}
if (_defaults != null)
{
// Don't return features masked by the wrapper.
foreach (var pair in _features == null ? _defaults : _defaults.Except(_features, FeatureKeyComparer))
{
yield return pair;
}
}
}
public TFeature? Get<TFeature>()
{
return (TFeature?)this[typeof(TFeature)];
}
public void Set<TFeature>(TFeature instance)
{
this[typeof(TFeature)] = instance;
}
private class KeyComparer : IEqualityComparer<KeyValuePair<Type, object>>
{
public bool Equals(KeyValuePair<Type, object> x, KeyValuePair<Type, object> y)
{
return x.Key.Equals(y.Key);
}
public int GetHashCode(KeyValuePair<Type, object> obj)
{
return obj.Key.GetHashCode();
}
}
}
}