From 6ef2fdb7895b79d12a5fc30c52176c7e3b94d790 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Tue, 1 Sep 2009 19:11:19 +0000 Subject: [PATCH] Fixed a typo; fixed a bug in the plug-in library that would see generic types deriving from a required base class as suitable whereas they cannot be instantiated without a generic argument; added unit tests that reproduce the errors git-svn-id: file:///srv/devel/repo-conversion/nusu@171 d2e56fa2-650e-0410-a79f-9358c0239efd --- Source/Plugins/FactoryEmployer.Test.cs | 23 +++++++++++++++++++++++ Source/Plugins/FactoryEmployer.cs | 8 +++++++- Source/Plugins/InstanceEmployer.Test.cs | 23 +++++++++++++++++++++++ Source/Plugins/InstanceEmployer.cs | 3 ++- Source/Tracking/Transaction.cs | 2 +- 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Source/Plugins/FactoryEmployer.Test.cs b/Source/Plugins/FactoryEmployer.Test.cs index beb5135..ffcd579 100644 --- a/Source/Plugins/FactoryEmployer.Test.cs +++ b/Source/Plugins/FactoryEmployer.Test.cs @@ -50,6 +50,16 @@ namespace Nuclex.Support.Plugins { #endregion // class Derived + #region class GenericDerived + + /// + /// Generic class derived from the abstract base to serve as concrete product + /// for testing the factory employer + /// + private class GenericDerived : Base { } + + #endregion // class GenericDerived + #region class Unrelated /// Unrelated class used to test the factory employer @@ -112,6 +122,19 @@ namespace Nuclex.Support.Plugins { ); } + /// + /// Tests whether the factory employer throws an exception when it is asked to + /// employ a class that requires generic parameters for instantiation + /// + [Test] + public void TestThrowOnEmployGenericClass() { + FactoryEmployer testEmployer = new FactoryEmployer(); + + Assert.Throws( + delegate() { testEmployer.Employ(typeof(GenericDerived<>)); } + ); + } + /// /// Tests whether the factory employer can employ a class derived from the product /// diff --git a/Source/Plugins/FactoryEmployer.cs b/Source/Plugins/FactoryEmployer.cs index c316330..2fd8ab6 100644 --- a/Source/Plugins/FactoryEmployer.cs +++ b/Source/Plugins/FactoryEmployer.cs @@ -91,7 +91,8 @@ namespace Nuclex.Support.Plugins { public override bool CanEmploy(Type type) { return PluginHelper.HasDefaultConstructor(type) && - typeof(ProductType).IsAssignableFrom(type); + typeof(ProductType).IsAssignableFrom(type) && + !type.ContainsGenericParameters; } /// Employs the specified plugin type @@ -107,6 +108,11 @@ namespace Nuclex.Support.Plugins { "Cannot employ type because it cannot be cast to the factory's product type" ); } + if(type.ContainsGenericParameters) { + throw new ArgumentException( + "Cannot employ type because it requires generic parameters", "type" + ); + } this.employedFactories.Add(new ConcreteFactory(type)); } diff --git a/Source/Plugins/InstanceEmployer.Test.cs b/Source/Plugins/InstanceEmployer.Test.cs index 9e546b5..8959f2c 100644 --- a/Source/Plugins/InstanceEmployer.Test.cs +++ b/Source/Plugins/InstanceEmployer.Test.cs @@ -50,6 +50,16 @@ namespace Nuclex.Support.Plugins { #endregion // class Derived + #region class GenericDerived + + /// + /// Generic class derived from the abstract base to serve as concrete product + /// for testing the instance employer + /// + private class GenericDerived : Base { } + + #endregion // class GenericDerived + #region class Unrelated /// Unrelated class used to test the instance employer @@ -96,6 +106,19 @@ namespace Nuclex.Support.Plugins { ); } + /// + /// Tests whether the instance employer throws an exception when it is asked to + /// employ a class that requires generic parameters for instantiation + /// + [Test] + public void TestThrowOnEmployGenericClass() { + InstanceEmployer testEmployer = new InstanceEmployer(); + + Assert.Throws( + delegate() { testEmployer.Employ(typeof(GenericDerived<>)); } + ); + } + /// /// Tests whether the instance employer can employ a class derived from the product /// diff --git a/Source/Plugins/InstanceEmployer.cs b/Source/Plugins/InstanceEmployer.cs index 5bac170..d8e9dbe 100644 --- a/Source/Plugins/InstanceEmployer.cs +++ b/Source/Plugins/InstanceEmployer.cs @@ -62,7 +62,8 @@ namespace Nuclex.Support.Plugins { public override bool CanEmploy(Type type) { return PluginHelper.HasDefaultConstructor(type) && - typeof(T).IsAssignableFrom(type); + typeof(T).IsAssignableFrom(type) && + !type.ContainsGenericParameters; } /// Employs the specified plugin type diff --git a/Source/Tracking/Transaction.cs b/Source/Tracking/Transaction.cs index 09ecfaf..29a63e4 100644 --- a/Source/Tracking/Transaction.cs +++ b/Source/Tracking/Transaction.cs @@ -246,7 +246,7 @@ namespace Nuclex.Support.Tracking { } - /// List of event handler which have subscribed to the ended event + /// Event handlers which have subscribed to the ended event /// /// Does not need to be volatile since it's only accessed inside ///