RegisterType (typeof) vs RegisterType

本文关键字:RegisterType vs typeof | 更新日期: 2023-09-27 18:08:01

下面的两段代码做完全相同的事情吗?

.RegisterType<ICrmService, CrmService>()

.RegisterType(typeof(ICrmService), typeof(CrmService))

RegisterType (typeof) vs RegisterType

总之:是的。

查看源代码:https://unity.codeplex.com/SourceControl/latest#source/Unity/Src/UnityContainerExtensions.cs

泛型方法:

public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, params InjectionMember[] injectionMembers) where TTo : TFrom
{
    Guard.ArgumentNotNull(container, "container");
    return container.RegisterType(typeof(TFrom), typeof(TTo), null, null, injectionMembers);
}
非泛型方法:
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, params InjectionMember[] injectionMembers)
{
    Guard.ArgumentNotNull(container, "container");
    return container.RegisterType(from, to, null, null, injectionMembers);
}