From 01bf16644b79581b6db1e7e9bf85353512ade679 Mon Sep 17 00:00:00 2001 From: Travis Illig Date: Tue, 1 Oct 2024 10:39:51 -0700 Subject: [PATCH] Update nullable annotations. --- .../MoqRegistrationHandler.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Autofac.Extras.Moq/MoqRegistrationHandler.cs b/src/Autofac.Extras.Moq/MoqRegistrationHandler.cs index 142c979..ee8e160 100644 --- a/src/Autofac.Extras.Moq/MoqRegistrationHandler.cs +++ b/src/Autofac.Extras.Moq/MoqRegistrationHandler.cs @@ -19,7 +19,13 @@ internal class MoqRegistrationHandler : IRegistrationSource private readonly ISet _createdServiceTypes; private readonly ISet _mockedServiceTypes; - private readonly MethodInfo _createMethod; + /// + /// This is with zero parameters. This + /// is important because it limits what can be auto-mocked. (MockFactory got + /// renamed to MockRepository but the method reference is still internally + /// on MockFactory.) + /// + private readonly MethodInfo _createMethod = typeof(MockRepository).GetMethod(nameof(MockRepository.Create), Array.Empty()) ?? throw new NotSupportedException("Unable to bind to Create method."); /// /// Initializes a new instance of the class. @@ -30,11 +36,6 @@ public MoqRegistrationHandler(ISet createdServiceTypes, ISet mockedS { _createdServiceTypes = createdServiceTypes; _mockedServiceTypes = mockedServiceTypes; - - // This is MockRepository.Create() with zero parameters. This is important because - // it limits what can be auto-mocked. - var factoryType = typeof(MockRepository); - _createMethod = factoryType.GetMethod(nameof(MockRepository.Create), Array.Empty()); } /// @@ -215,13 +216,13 @@ private object CreateMock(IComponentContext context, TypedService typedService) try { var specificCreateMethod = _createMethod.MakeGenericMethod(new[] { typedService.ServiceType }); - var mock = (Mock)specificCreateMethod.Invoke(context.Resolve(), null); + var mock = (Mock)specificCreateMethod.Invoke(context.Resolve(), null)!; return mock.Object; } catch (TargetInvocationException ex) { // Expose the inner exception as if it was directly thrown. - ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); + ExceptionDispatchInfo.Capture(ex.InnerException!).Throw(); // Won't get here, but the compiler doesn't know that. throw ex.InnerException;