From ceb8dd95a8c6a3850614214686136cc67664fe7d Mon Sep 17 00:00:00 2001 From: cneuwirt Date: Sun, 2 Mar 2008 01:18:16 +0000 Subject: [PATCH] Added testcase for generic registrations and a workaround for the CLR GetInterfaces() bug in which interfaces with no implementations are returned. git-svn-id: https://svn.castleproject.org/svn/castle/trunk@4866 73e77b4c-caa6-f847-a29a-24ab75ae54b6 --- .../ClassComponents/Repository.cs | 15 +++++++++++++++ .../Registration/AllTypesOfTestCase.cs | 19 ++++++++++++++++--- .../Registration/Strategies/AllTypesOf.cs | 6 +++++- .../Registration/Strategies/ServiceDescriptor.cs | 20 +++++++++++++------- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/Repository.cs b/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/Repository.cs index 2981d1f8c..4985f5038 100644 --- a/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/Repository.cs +++ b/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/Repository.cs @@ -18,6 +18,12 @@ namespace Castle.MicroKernel.Tests.ClassComponents { } + public interface IRepository : IRepository + where T : class + { + T Find(); + } + public class DecoratedRepository : IRepository { public DecoratedRepository() @@ -69,4 +75,13 @@ namespace Castle.MicroKernel.Tests.ClassComponents _inner = inner; } } + + public class DefaultRepository : IRepository + where T : class, new() + { + public T Find() + { + return new T(); + } + } } \ No newline at end of file diff --git a/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs b/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs index b26ad89c6..5ff758628 100644 --- a/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs +++ b/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs @@ -97,9 +97,22 @@ namespace Castle.MicroKernel.Tests.Registration Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType); Assert.AreEqual(handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name); } - } - - #if DOTNET35 + } + + [Test] + public void RegisterGenericTypes_WithGenericDefinition_RegisteredInContainer() + { + kernel.Register(AllTypes + .From(typeof(DefaultRepository<>)) + .WithService.FirstInterface() + ); + + Type t = typeof(IRepository); + + IRepository repository = kernel.Resolve>(); + } + +#if DOTNET35 [Test] public void RegisterAssemblyTypes_IfCondition_RegisteredInContainer() diff --git a/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs b/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs index a157472d6..85b57fd36 100644 --- a/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs +++ b/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs @@ -23,7 +23,7 @@ namespace Castle.MicroKernel.Registration /// The starting point to describe types to register. /// /// The base type to match against. - public static class AllTypesOf + public class AllTypesOf { /// /// Prepares to register types from an assembly. @@ -98,4 +98,8 @@ namespace Castle.MicroKernel.Registration return new TypesDescriptor(types); } } + + public class AllTypes : AllTypesOf + { + } } diff --git a/InversionOfControl/Castle.MicroKernel/Registration/Strategies/ServiceDescriptor.cs b/InversionOfControl/Castle.MicroKernel/Registration/Strategies/ServiceDescriptor.cs index 55cf49703..b3ef43c56 100644 --- a/InversionOfControl/Castle.MicroKernel/Registration/Strategies/ServiceDescriptor.cs +++ b/InversionOfControl/Castle.MicroKernel/Registration/Strategies/ServiceDescriptor.cs @@ -38,10 +38,7 @@ namespace Castle.MicroKernel.Registration /// public TypesDescriptor Base() { - return Select(delegate(Type type) - { - return typeof(T); - }); + return Select(delegate { return typeof(T); }); } /// @@ -52,12 +49,21 @@ namespace Castle.MicroKernel.Registration { return Select(delegate(Type type) { + Type first = null; Type[] interfaces = type.GetInterfaces(); if (interfaces.Length > 0) { - return interfaces[0]; + first = interfaces[0]; + + // This is a workaround for a CLR bug in + // which GetInterfaces() returns interfaces + // with no implementations. + if (first.IsGenericType && first.ReflectedType == null) + { + first = first.GetGenericTypeDefinition(); + } } - return null; + return first; }); } @@ -68,7 +74,7 @@ namespace Castle.MicroKernel.Registration /// public TypesDescriptor Select(ServiceSelector selector) { - this.serviceSelector = selector; + serviceSelector = selector; return typesDescriptor; } -- 2.11.4.GIT