代码拉取完成,页面将自动刷新
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Demo
{
public interface IFeatureCollection : IEnumerable<KeyValuePair<Type, object>>
{
bool IsReadOnly { get; }
int Revision { get; }
object this[Type key] { get; set; }
TFeature Get<TFeature>();
void Set<TFeature>(TFeature instance);
}
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();
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。