Skip to content

Commit 58d1278

Browse files
Add an exception for non interface registrations.
1 parent caae736 commit 58d1278

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

LazyProxy.Unity.Tests/UnityExtensionTests.cs

+6
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,11 @@ public void ServicesMustBeResolvedByNameWithCorrectInjectionMembers()
275275
Assert.Equal("service1->" + result1, actualResult1);
276276
Assert.Equal("service1->" + result2, actualResult2);
277277
}
278+
279+
[Fact]
280+
public void RegistrationMustThrowAnExceptionForNonInterfaces()
281+
{
282+
Assert.Throws<NotSupportedException>(() => new UnityContainer().RegisterLazy<Service1, Service1>());
283+
}
278284
}
279285
}

LazyProxy.Unity/UnityExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public static IUnityContainer RegisterLazy<TFrom, TTo>(this IUnityContainer cont
7373
params InjectionMember[] injectionMembers)
7474
where TTo : TFrom where TFrom : class
7575
{
76+
// There is no way to constraint it on the compilation step.
77+
if (!typeof(TFrom).IsInterface)
78+
{
79+
throw new NotSupportedException("The lazy registration is supported only for interfaces.");
80+
}
81+
7682
var lazyProxyType = LazyProxyBuilder.BuildLazyProxyType<TFrom>();
7783
var registrationName = Guid.NewGuid().ToString();
7884

0 commit comments

Comments
 (0)