Class 当 usings 在命名空间内部而不是外部时,无法通过反射找到。为什么?
Class cannot be found by reflection when usings are inside namespace and not outside. Why?
我们有一些处理程序,这些处理程序是通过反射在 IoC 中实例化的,因为它们无法被引用。在我使用 StyleCop 并将一些处理程序的使用移到命名空间内之前,这一直很好用。突然,我们的算法无法获得特定命令的处理程序。我发现这个看似微不足道的变化却起了作用。
更改前的处理程序:
using System;
using System.Threading;
using System.Threading.Tasks;
using AccessManagement.Common.Configuration;
using AccessManagement.DirectoryServices;
using AccessManagement.Management.AdContact.Commands;
using AccessManagement.Management.AdUser;
using AccessManagement.{NotForYourEyes1};
using AccessManagement.{NotForYourEyes1}.Enums;
using AccessManagement.{NotForYourEyes1}.Extensions;
using AccessManagement.{NotForYourEyes1}.Models;
using Architecture.Core;
using {NotForYourEyes2}.Common;
using {NotForYourEyes2}.Common.Configuration;
namespace AccessManagement.Management.AdContact
{
[EnableHandler]
public class MoveAdContactHandler : IHandleRequest<MoveAdContact, AdUserResponse>
{
public Task<AdUserResponse> Handle(MoveAdContact request, CancellationToken cancellationToken = new CancellationToken())
{
更改后的处理程序:
namespace AccessManagement.Management.AdContact
{
using System;
using System.Threading;
using System.Threading.Tasks;
using AccessManagement.Common.Configuration;
using AccessManagement.DirectoryServices;
using AccessManagement.Management.AdContact.Commands;
using AccessManagement.Management.AdUser;
using AccessManagement.{NotForYourEyes1};
using AccessManagement.{NotForYourEyes1}.Enums;
using AccessManagement.{NotForYourEyes1}.Extensions;
using AccessManagement.{NotForYourEyes1}.Models;
using Architecture.Core;
using {NotForYourEyes2}.Common;
using {NotForYourEyes2}.Common.Configuration;
[EnableHandler]
public class MoveAdContact2Handler : IHandleRequest<MoveAdContact, AdUserResponse>
{
向 IoC 容器注册处理程序的算法:
private static void RegisterBusHandlers(IServiceRegistry serviceRegistry, Func<ILifetime> handlersLifetime)
{
var handlerProvider = new HandlerRegistrator((i, t) =>
{
serviceRegistry.Register(i, t, handlersLifetime());
});
var handlerTypes =
AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.GetCustomAttribute<EnableHandlerAttribute>() != null);
foreach (var handlerType in handlerTypes)
{
handlerProvider.RegisterHandler(handlerType);
}
}
总而言之 -- 找到更改之前的处理程序很好,根本找不到更改之后的处理程序。这对我和我的团队来说有点像脑筋急转弯。谁能解释一下这种行为?
那些 using
指令现在将 在 考虑封闭命名空间之前使用。您可能曾经将类型定位在 AccessManagement.Management
或 AccessManagement
中,而现在将相同的类型名称解析为那些 using
指令声明的名称空间中的类型。
哪些只有你自己能决定。
例如如果有两种不同的 EnableHandlerAttribute
类型,那么您的注册码现在可能正在注册完全不同的类型。
我们有一些处理程序,这些处理程序是通过反射在 IoC 中实例化的,因为它们无法被引用。在我使用 StyleCop 并将一些处理程序的使用移到命名空间内之前,这一直很好用。突然,我们的算法无法获得特定命令的处理程序。我发现这个看似微不足道的变化却起了作用。
更改前的处理程序:
using System;
using System.Threading;
using System.Threading.Tasks;
using AccessManagement.Common.Configuration;
using AccessManagement.DirectoryServices;
using AccessManagement.Management.AdContact.Commands;
using AccessManagement.Management.AdUser;
using AccessManagement.{NotForYourEyes1};
using AccessManagement.{NotForYourEyes1}.Enums;
using AccessManagement.{NotForYourEyes1}.Extensions;
using AccessManagement.{NotForYourEyes1}.Models;
using Architecture.Core;
using {NotForYourEyes2}.Common;
using {NotForYourEyes2}.Common.Configuration;
namespace AccessManagement.Management.AdContact
{
[EnableHandler]
public class MoveAdContactHandler : IHandleRequest<MoveAdContact, AdUserResponse>
{
public Task<AdUserResponse> Handle(MoveAdContact request, CancellationToken cancellationToken = new CancellationToken())
{
更改后的处理程序:
namespace AccessManagement.Management.AdContact
{
using System;
using System.Threading;
using System.Threading.Tasks;
using AccessManagement.Common.Configuration;
using AccessManagement.DirectoryServices;
using AccessManagement.Management.AdContact.Commands;
using AccessManagement.Management.AdUser;
using AccessManagement.{NotForYourEyes1};
using AccessManagement.{NotForYourEyes1}.Enums;
using AccessManagement.{NotForYourEyes1}.Extensions;
using AccessManagement.{NotForYourEyes1}.Models;
using Architecture.Core;
using {NotForYourEyes2}.Common;
using {NotForYourEyes2}.Common.Configuration;
[EnableHandler]
public class MoveAdContact2Handler : IHandleRequest<MoveAdContact, AdUserResponse>
{
向 IoC 容器注册处理程序的算法:
private static void RegisterBusHandlers(IServiceRegistry serviceRegistry, Func<ILifetime> handlersLifetime)
{
var handlerProvider = new HandlerRegistrator((i, t) =>
{
serviceRegistry.Register(i, t, handlersLifetime());
});
var handlerTypes =
AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.GetCustomAttribute<EnableHandlerAttribute>() != null);
foreach (var handlerType in handlerTypes)
{
handlerProvider.RegisterHandler(handlerType);
}
}
总而言之 -- 找到更改之前的处理程序很好,根本找不到更改之后的处理程序。这对我和我的团队来说有点像脑筋急转弯。谁能解释一下这种行为?
那些 using
指令现在将 在 考虑封闭命名空间之前使用。您可能曾经将类型定位在 AccessManagement.Management
或 AccessManagement
中,而现在将相同的类型名称解析为那些 using
指令声明的名称空间中的类型。
哪些只有你自己能决定。
例如如果有两种不同的 EnableHandlerAttribute
类型,那么您的注册码现在可能正在注册完全不同的类型。