From 0637b9d71bdbf793410c02e607b3a0dcf97c2810 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Thu, 8 Mar 2012 17:14:42 +0000 Subject: [PATCH] Moved another helper method that tests whether a type is carrying an attribute into the TypeHelper class git-svn-id: file:///srv/devel/repo-conversion/nusu@271 d2e56fa2-650e-0410-a79f-9358c0239efd --- Source/Plugins/NoPluginAttribute.Test.cs | 4 ++-- Source/Plugins/PluginHost.cs | 17 +---------------- Source/TypeHelper.Test.cs | 17 +++++++++++++++++ Source/TypeHelper.cs | 11 +++++++++++ 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Source/Plugins/NoPluginAttribute.Test.cs b/Source/Plugins/NoPluginAttribute.Test.cs index 0fef369..0a51fda 100644 --- a/Source/Plugins/NoPluginAttribute.Test.cs +++ b/Source/Plugins/NoPluginAttribute.Test.cs @@ -35,8 +35,8 @@ namespace Nuclex.Support.Plugins { /// Tests whether the default consturctor of the no plugin attribute works /// [Test] - public void TestDefaultConstructor() { - new NoPluginAttribute(); + public void HasDefaultConstructor() { + Assert.NotNull(new NoPluginAttribute()); } } diff --git a/Source/Plugins/PluginHost.cs b/Source/Plugins/PluginHost.cs index e0287d5..0344d07 100644 --- a/Source/Plugins/PluginHost.cs +++ b/Source/Plugins/PluginHost.cs @@ -86,8 +86,7 @@ namespace Nuclex.Support.Plugins { } // Types that have been tagged with the [NoPlugin] attribute will be ignored - object[] attributes = type.GetCustomAttributes(true); - if(containsNoPluginAttribute(attributes)) { + if(type.HasAttribute()) { continue; } @@ -104,20 +103,6 @@ namespace Nuclex.Support.Plugins { } - /// - /// Determines whether the specifies list of attributes contains a NoPluginAttribute - /// - /// List of attributes to check - /// True if the list contained a NoPluginAttribute, false otherwise - private static bool containsNoPluginAttribute(object[] attributes) { - for(int index = 0; index < attributes.Length; ++index) { - if(attributes[index] is NoPluginAttribute) { - return true; - } - } - return false; - } - /// Reports an error to the debugging console /// Error message that will be reported private static void reportError(string error) { diff --git a/Source/TypeHelper.Test.cs b/Source/TypeHelper.Test.cs index 050a9a8..b993784 100644 --- a/Source/TypeHelper.Test.cs +++ b/Source/TypeHelper.Test.cs @@ -86,6 +86,23 @@ namespace Nuclex.Support { #endregion // class Derived + #region class HasIgnoreAttribute + + /// Class that carries an IgnoreAttribute + [Ignore] + private class HasIgnoreAttribute { } + + #endregion // class HasIgnoreAttribute + + /// + /// Verifies that the type helper can determine whether a class is carrying an attribute + /// + [Test] + public void CanDetermineIfTypeHasAttribute() { + Assert.IsTrue(typeof(HasIgnoreAttribute).HasAttribute()); + Assert.IsFalse(typeof(HasIgnoreAttribute).HasAttribute()); + } + /// /// Verifies that the GetFieldInfosIncludingBaseClasses() will include the backing /// fields of automatically implemented properties in base classes diff --git a/Source/TypeHelper.cs b/Source/TypeHelper.cs index 0ceee0f..6592f73 100644 --- a/Source/TypeHelper.cs +++ b/Source/TypeHelper.cs @@ -167,6 +167,17 @@ namespace Nuclex.Support { return false; } + /// Determines whether the type has the specified attribute + /// Attribute the type will be checked for + /// + /// Type that will be checked for presence of the specified attribute + /// + /// True if the type has the specified attribute, otherwise false + public static bool HasAttribute(this Type type) { + object[] attributes = type.GetCustomAttributes(typeof(TAttribute), true); + return (attributes != null) && (attributes.Length > 0); + } + } } // namespace Nuclex.Support