From 9cfa890151e87c965eebe3f0569a94f3bb309b46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Intentor=20Martins?= Date: Thu, 2 Apr 2015 14:25:33 -0300 Subject: [PATCH] Updated CommanderExtension.RegisterCommands() to use TypeUtils.GetAssignableTypesInNamespace() to get commands from the current assembly(ies). --- src/Assets/Adic/CHANGELOG.txt | 3 +++ .../Commander/CommanderExtension.cs | 22 ++++++------------- .../Adic/Scripts/Framework/Util/TypeUtils.cs | 20 +++++++++++++++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Assets/Adic/CHANGELOG.txt b/src/Assets/Adic/CHANGELOG.txt index e139af9..3616676 100644 --- a/src/Assets/Adic/CHANGELOG.txt +++ b/src/Assets/Adic/CHANGELOG.txt @@ -10,6 +10,9 @@ Version 2.5 (2015-04-02) Framework - Added creation and injection of factories by the container. +Commander extension +- Updated CommanderExtension.RegisterCommands() to use TypeUtils.GetAssignableTypesInNamespace() to get commands from the current assembly(ies). + Version 2.4 (2015-04-01) Framework diff --git a/src/Assets/Adic/Scripts/Extensions/Commander/CommanderExtension.cs b/src/Assets/Adic/Scripts/Extensions/Commander/CommanderExtension.cs index 17a357d..fa8ced6 100644 --- a/src/Assets/Adic/Scripts/Extensions/Commander/CommanderExtension.cs +++ b/src/Assets/Adic/Scripts/Extensions/Commander/CommanderExtension.cs @@ -1,10 +1,9 @@ using System; using System.Collections; -using System.Linq; -using System.Reflection; using Adic.Commander.Exceptions; using Adic.Container; using Adic.Exceptions; +using Adic.Util; namespace Adic { /// @@ -66,20 +65,13 @@ public static void RegisterCommands(this IInjectionContainer container, string n /// The container in which the command will be registered. /// Indicates whether child namespaces should be included. /// Namespace name. - public static void RegisterCommands(this IInjectionContainer container, string namespaceName, bool includeChildren) { - var commandType = typeof(ICommand); - var commands = - from t in Assembly.GetExecutingAssembly().GetTypes() - where t.IsClass && - ( - (includeChildren && !string.IsNullOrEmpty(t.Namespace) && t.Namespace.StartsWith(namespaceName)) || - (!includeChildren && t.Namespace == namespaceName) - ) && - commandType.IsAssignableFrom(t) - select t; + public static void RegisterCommands(this IInjectionContainer container, + string namespaceName, + bool includeChildren) { + var commands = TypeUtils.GetAssignableTypesInNamespace(typeof(ICommand), namespaceName, includeChildren); - foreach (var type in commands) { - container.Bind().To(type); + for (var cmdIndex = 0; cmdIndex < commands.Length; cmdIndex++) { + container.Bind().To(commands[cmdIndex]); } PoolCommands(container); diff --git a/src/Assets/Adic/Scripts/Framework/Util/TypeUtils.cs b/src/Assets/Adic/Scripts/Framework/Util/TypeUtils.cs index 8d8b64e..80dc147 100644 --- a/src/Assets/Adic/Scripts/Framework/Util/TypeUtils.cs +++ b/src/Assets/Adic/Scripts/Framework/Util/TypeUtils.cs @@ -17,7 +17,7 @@ public static class TypeUtils { public static bool IsAssignable(Type potentialBase, Type potentialDescendant) { return potentialBase.IsAssignableFrom(potentialDescendant); } - + /// /// Gets all types assignable from a given /// in a given . @@ -26,6 +26,18 @@ public static bool IsAssignable(Type potentialBase, Type potentialDescendant) { /// Namespace name. /// The assignable types in the namespace. public static Type[] GetAssignableTypesInNamespace(Type baseType, string namespaceName) { + return GetAssignableTypesInNamespace(baseType, namespaceName, false); + } + + /// + /// Gets all types assignable from a given + /// in a given . + /// + /// Base type from which the types in the namespace must be assignable. + /// Namespace name. + /// Indicates whether child namespaces should be included. + /// The assignable types in the namespace. + public static Type[] GetAssignableTypesInNamespace(Type baseType, string namespaceName, bool includeChildren) { var typesToBind = new List(); //Fills the assembly list. @@ -50,7 +62,11 @@ public static Type[] GetAssignableTypesInNamespace(Type baseType, string namespa for (int typeIndex = 0; typeIndex < allTypes.Length; typeIndex++) { var type = allTypes[typeIndex]; - if (type.Namespace == namespaceName && + var isTypeInNamespace = + (includeChildren && !string.IsNullOrEmpty(type.Namespace) && type.Namespace.StartsWith(namespaceName)) || + (!includeChildren && type.Namespace == namespaceName); + + if (isTypeInNamespace && type.IsClass && TypeUtils.IsAssignable(baseType, type)) { typesToBind.Add(type);