RegisterType(typeof)vs RegisterType

时间:2016-09-20 18:47:27

标签: c# .net unity-container ioc-container

以下两段代码是否完全相同?

.RegisterType<ICrmService, CrmService>()

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

1 个答案:

答案 0 :(得分:2)

简而言之:是的,他们确实这样做了。

只需查看源代码: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);
}