diff --git a/src/Moryx.Resources.Management/Resources/ResourceTypeController.cs b/src/Moryx.Resources.Management/Resources/ResourceTypeController.cs index 893ef29a..5140fb21 100644 --- a/src/Moryx.Resources.Management/Resources/ResourceTypeController.cs +++ b/src/Moryx.Resources.Management/Resources/ResourceTypeController.cs @@ -264,17 +264,19 @@ private ResourceProxy InstantiateProxy(string typeName, Resource instance) /// private void ProvideProxyType(Type resourceType) { - // Step 1: Find the least specific base type that offers the same amount of interfaces + // Step 1: Find the least specific base type that offers the same amount of interfaces and is not a generic itself // ReSharper disable once AssignNullToNotNullAttribute -> FullName should be not null var targetType = _typeCache[resourceType.ResourceType()]; var linker = targetType; var interfaces = RelevantInterfaces(linker); - // Move up the type tree until the parent offers less interfaces than the current linker - while (linker.BaseType != null && interfaces.Count == RelevantInterfaces(linker.BaseType).Count) + // Move up the type tree until the parent offers less interfaces than the current linker, is abstract or a generic + while (linker.BaseType != null && !linker.BaseType.ResourceType.IsGenericType + && interfaces.Count == RelevantInterfaces(linker.BaseType).Count) { linker = linker.BaseType; } + // Step 2: Check if we already created a proxy for this type. If we already // did use this one for the requested type as well. diff --git a/src/Tests/Moryx.Resources.Management.Tests/Mocks/ResourceWithGenericMethod.cs b/src/Tests/Moryx.Resources.Management.Tests/Mocks/ResourceWithGenericMethod.cs index ee751b56..d8574ca9 100644 --- a/src/Tests/Moryx.Resources.Management.Tests/Mocks/ResourceWithGenericMethod.cs +++ b/src/Tests/Moryx.Resources.Management.Tests/Mocks/ResourceWithGenericMethod.cs @@ -57,4 +57,10 @@ public void RaiseEvent() throw new NotImplementedException(); } } + + public interface IGenericBaseResourceInterface : IResource { } + + public class GenericBaseResource : Resource, IGenericBaseResourceInterface { } + + public class InheritingFromGenericResource : GenericBaseResource { } } diff --git a/src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs b/src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs index 8ef3f51f..ccb8a8f9 100644 --- a/src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs +++ b/src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs @@ -222,6 +222,20 @@ public void ProxyBuilderFiltersGenericInterfaces() Assert.IsFalse(typeof(IGenericMethodCall).IsAssignableFrom(proxy.GetType())); } + [Test] + public void ProxyBuilderSkipsGenericBaseTypes() + { + // Arrange + var driver = new InheritingFromGenericResource { Id = 42, Name = "A non generic resource inheriting from a generic base type" }; + + // Act + var proxy = _typeController.GetProxy(driver); + + // Assert + Assert.IsNotNull(proxy); + Assert.IsFalse(typeof(GenericBaseResource).IsAssignableFrom(proxy.GetType())); + } + [Test] public void FacadeExceptionForGenericProxy() {