< Summary - PropertyGridHelpers Code Coverage

Information
Class: PropertyGridHelpers.ServiceProviders.CustomServiceProvider
Assembly: PropertyGridHelpers
File(s): c:\agent\_work\9\s\Code\PropertyGridHelpers\ServiceProviders\CustomServiceProvider.cs
Tag: PropertyGridHelpers Build_2025.7.15.1_#485
Line coverage
100%
Covered lines: 7
Uncovered lines: 0
Coverable lines: 7
Total lines: 54
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBlocks covered Blocks not covered Branch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()--100%11100%
CustomServiceProvider()30----
AddService(...)20----
AddService(...)--100%11100%
GetService(...)30100%11100%
GetService(...)20----

File(s)

c:\agent\_work\9\s\Code\PropertyGridHelpers\ServiceProviders\CustomServiceProvider.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace PropertyGridHelpers.ServiceProviders
 5{
 6
 7    /// <summary>
 8    /// A lightweight implementation of <see cref="IServiceProvider"/> that allows manual registration
 9    /// and retrieval of service instances by type.
 10    /// </summary>
 11    /// <remarks>
 12    /// This class is useful in testing scenarios or situations where full-fledged dependency injection
 13    /// is unnecessary. It is also used internally by the <c>PropertyGridHelpers</c> library to support
 14    /// dynamic or programmatic access to services that would otherwise be resolved via attributes.
 15    /// </remarks>
 16    /// <example>
 17    /// <code>
 18    /// var provider = new CustomServiceProvider();
 19    /// provider.AddService(typeof(IMyService), new MyService());
 20    /// var service = provider.GetService(typeof(IMyService)) as IMyService;
 21    /// </code>
 22    /// </example>
 23    public class CustomServiceProvider : IServiceProvider
 24    {
 25#if NET8_0_OR_GREATER
 7226        private readonly Dictionary<Type, object> _services = [];
 27#else
 1628        private readonly Dictionary<Type, object> _services = new Dictionary<Type, object>();
 29#endif
 30
 31        /// <summary>
 32        /// Registers a service instance with the specified service type.
 33        /// </summary>
 34        /// <param name="serviceType">The type that identifies the service.</param>
 35        /// <param name="serviceInstance">The instance of the service to associate with the type.</param>
 36        /// <exception cref="ArgumentNullException">
 37        /// Thrown if <paramref name="serviceType"/> or <paramref name="serviceInstance"/> is <c>null</c>.
 38        /// </exception>
 6439        public void AddService(Type serviceType, object serviceInstance) => _services[serviceType] = serviceInstance;
 40
 41        /// <summary>
 42        /// Retrieves a service object of the specified type.
 43        /// </summary>
 44        /// <param name="serviceType">An object that specifies the type of service object to retrieve.</param>
 45        /// <returns>
 46        /// A service object of type <paramref name="serviceType"/>, or <c>null</c> if no service of that type is regist
 47        /// </returns>
 48        public object GetService(Type serviceType)
 4849        {
 5650            _ = _services.TryGetValue(serviceType, out var service);
 5651            return service;
 4852        }
 53    }
 54}