< Summary - Core.Tests

Information
Class: Common.Core.Classes.InterfaceFactory
Assembly: Common.Core
File(s): D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Classes\InterfaceFactory.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 10
Uncovered lines: 0
Coverable lines: 10
Total lines: 46
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

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_ConcreteType()100%11100%
get_InterfaceType()100%11100%
CanConvert(...)100%11100%
CreateConverter(...)100%11100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Classes\InterfaceFactory.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Common.Core.Classes;
 5
 6/// <summary>Initializes a new instance of the InterfaceFactory class.</summary>
 7/// <param name="concrete">The concrete class type.</param>
 8/// <param name="interfaceType">The interface type.</param>
 9/// <remarks>Supports converting Interface types for Json serialization by using a factory pattern.</remarks>
 410public class InterfaceFactory( Type concrete, Type interfaceType ) : JsonConverterFactory
 11{
 12  #region Properties
 13
 14  /// <summary>Gets the concrete class type.</summary>
 515  public Type ConcreteType { get; } = concrete;
 16
 17  /// <summary>Gets the interface type.</summary>
 618  public Type InterfaceType { get; } = interfaceType;
 19
 20  #endregion
 21
 22  #region Overridden Methods
 23
 24  /// <summary>Determines whether the converter instance can convert the specified object type.</summary>
 25  /// <param name="typeToConvert">The type of the object to check whether it can be
 26  /// converted by this converter instance.</param>
 27  /// <returns><see langword="true"/> if the instance can convert the specified object type otherwise
 28  /// <see langword="false"/>.</returns>
 29  public override bool CanConvert( Type typeToConvert )
 130  {
 131    return typeToConvert == InterfaceType;
 132  }
 33
 34  /// <summary>Creates a converter for a specified type.</summary>
 35  /// <param name="typeToConvert">The type handled by the converter.</param>
 36  /// <param name="options">The serialization options to use.</param>
 37  /// <returns>A converter compatible with typeToConvert.</returns>
 38  public override JsonConverter CreateConverter( Type typeToConvert, JsonSerializerOptions options )
 139  {
 140    var converterType = typeof( InterfaceConverter<,> ).MakeGenericType( ConcreteType, InterfaceType );
 41
 142    return (JsonConverter)Activator.CreateInstance( converterType )!;
 143  }
 44
 45  #endregion
 46}