|
通过这个方法我们就可以看到其实注册单类型的方法,也是通过调用的注入实例到抽象的方法,只不过是将自己注册给了自己。好了,抽象和扩展方法我们就先说到这里,接下来我们来看IServiceCollection的实现类ServiceCollection的实现Copypublic class ServiceCollection : IServiceCollection{ private readonly List _descriptors = new List(); public int Count => _descriptors.Count; public bool IsReadOnly => false; public ServiceDescriptor this[int index] { get { return _descriptors[index]; } set { _descriptors[index] = value; } } public void Clear() { _descriptors.Clear(); } public bool Contains(ServiceDescriptor item) { return _descriptors.Contains(item); } public void CopyTo(ServiceDescriptor[] array, int arrayIndex) { _descriptors.CopyTo(array, arrayIndex); } public bool Remove(ServiceDescriptor item) { return _descriptors.Remove(item); } public IEnumerator GetEnumerator() { return _descriptors.GetEnumerator(); } void ICollection.Add(ServiceDescriptor item) { _descriptors.Add(item); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public int IndexOf(ServiceDescriptor item) { return _descriptors.IndexOf(item); } public void Insert(int index, ServiceDescriptor item) { _descriptors.Insert(index, item); } public void RemoveAt(int index) { _descriptors.RemoveAt(index); }}这个类就非常清晰,也非常简单了。ServiceCollection承载了一个List的集合,由于实现了IList接口,所以该类实现了接口的方法,实现了对List集合的操作,其核心就是ServiceDescriptor服务描述类,我们看一下大致的源码。
|
|