.net core 依赖注入源码位于.net core的runtime源码 中 ,其他源码信息
.net core独立模块源码:https://github.com/aspnet
.net core全家桶源码:https://github.com/dotnet/aspnetcore
.net core拓展库源码:https://github.com/dotnet/extensions
.net core标准库源码:https://github.com/dotnet/corefx
.net core的EFCore源码:https://github.com/dotnet/efcore
.net core的SDK源码:https://github.com/dotnet/sdk
.net core的runtime源码:https://github.com/dotnet/runtime
依赖注入模块主要的2个类 IServiceCollection 和 IServiceProvider
1.IServiceCollection源码
/// <summary> /// Specifies the contract for a collection of service descriptors. /// </summary> public interface IServiceCollection : IList<ServiceDescriptor> { }
[DebuggerDisplay("{DebuggerToString(),nq}")] [DebuggerTypeProxy(typeof(ServiceCollectionDebugView))] public class ServiceCollection : IServiceCollection { private readonly List<ServiceDescriptor> _descriptors = new List<ServiceDescriptor>(); private bool _isReadOnly; ..... }
ServiceCollection中包含一组 ServiceDescriptor,用于记录注册类的描述信息,
public class ServiceDescriptor { private ServiceDescriptor(Type serviceType, object? serviceKey, ServiceLifetime lifetime) { Lifetime = lifetime; ServiceType = serviceType; ServiceKey = serviceKey; } /// <summary> /// Gets the <see cref="ServiceLifetime"/> of the service. /// </summary> public ServiceLifetime Lifetime { get; } /// <summary> /// Get the key of the service, if applicable. /// </summary> public object? ServiceKey { get; } /// <summary> /// Gets the <see cref="Type"/> of the service. /// </summary> public Type ServiceType { get; } [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] private Type? _implementationType; /// <summary> /// Gets the <see cref="Type"/> that implements the service. /// </summary> [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] public Type? ImplementationType { get { if (IsKeyedService) { ThrowKeyedDescriptor(); } return _implementationType; } } /// <summary> /// Gets the <see cref="Type"/> that implements the service. /// </summary> [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] public Type? KeyedImplementationType { get { if (!IsKeyedService) { ThrowNonKeyedDescriptor(); } return _implementationType; } } private object? _implementationInstance; /// <summary> /// Gets the instance that implements the service. /// </summary> public object? ImplementationInstance { get { if (IsKeyedService) { ThrowKeyedDescriptor(); } return _implementationInstance; } } /// <summary> /// Gets the instance that implements the service. /// </summary> public object? KeyedImplementationInstance { get { if (!IsKeyedService) { ThrowNonKeyedDescriptor(); } return _implementationInstance; } } private object? _implementationFactory; /// <summary> /// Gets the factory used for creating service instances. /// </summary> public Func<IServiceProvider, object>? ImplementationFactory { get { if (IsKeyedService) { ThrowKeyedDescriptor(); } return (Func<IServiceProvider, object>?) _implementationFactory; } } /// <summary> /// Gets the factory used for creating Keyed service instances. /// </summary> public Func<IServiceProvider, object?, object>? KeyedImplementationFactory { get { if (!IsKeyedService) { ThrowNonKeyedDescriptor(); } return (Func<IServiceProvider, object?, object>?) _implementationFactory; } } /// <summary> /// Indicates whether the service is a keyed service. /// </summary> public bool IsKeyedService => ServiceKey != null; }
标签:core,service,get,源码,DependencyInjection,net,public From: https://www.cnblogs.com/871735097-/p/18334442