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