无名商城论坛

搜索
查看: 288|回复: 0

[其他技术] 【L·S】浅谈.Net Core Dependency 四

[复制链接]

1万

主题

1万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
32464
发表于 2022-5-8 17:32:19 | 显示全部楼层 |阅读模式
通过这个方法我们就可以看到其实注册单类型的方法,也是通过调用的注入实例到抽象的方法,只不过是将自己注册给了自己。好了,抽象和扩展方法我们就先说到这里,接下来我们来看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服务描述类,我们看一下大致的源码。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表