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
This commit is contained in:
Markus Ewald 2012-03-08 17:14:42 +00:00
parent 97de199705
commit 0637b9d71b
4 changed files with 31 additions and 18 deletions

View File

@ -35,8 +35,8 @@ namespace Nuclex.Support.Plugins {
/// Tests whether the default consturctor of the no plugin attribute works
/// </summary>
[Test]
public void TestDefaultConstructor() {
new NoPluginAttribute();
public void HasDefaultConstructor() {
Assert.NotNull(new NoPluginAttribute());
}
}

View File

@ -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<NoPluginAttribute>()) {
continue;
}
@ -104,20 +103,6 @@ namespace Nuclex.Support.Plugins {
}
/// <summary>
/// Determines whether the specifies list of attributes contains a NoPluginAttribute
/// </summary>
/// <param name="attributes">List of attributes to check</param>
/// <returns>True if the list contained a NoPluginAttribute, false otherwise</returns>
private static bool containsNoPluginAttribute(object[] attributes) {
for(int index = 0; index < attributes.Length; ++index) {
if(attributes[index] is NoPluginAttribute) {
return true;
}
}
return false;
}
/// <summary>Reports an error to the debugging console</summary>
/// <param name="error">Error message that will be reported</param>
private static void reportError(string error) {

View File

@ -86,6 +86,23 @@ namespace Nuclex.Support {
#endregion // class Derived
#region class HasIgnoreAttribute
/// <summary>Class that carries an IgnoreAttribute</summary>
[Ignore]
private class HasIgnoreAttribute { }
#endregion // class HasIgnoreAttribute
/// <summary>
/// Verifies that the type helper can determine whether a class is carrying an attribute
/// </summary>
[Test]
public void CanDetermineIfTypeHasAttribute() {
Assert.IsTrue(typeof(HasIgnoreAttribute).HasAttribute<IgnoreAttribute>());
Assert.IsFalse(typeof(HasIgnoreAttribute).HasAttribute<TestAttribute>());
}
/// <summary>
/// Verifies that the GetFieldInfosIncludingBaseClasses() will include the backing
/// fields of automatically implemented properties in base classes

View File

@ -167,6 +167,17 @@ namespace Nuclex.Support {
return false;
}
/// <summary>Determines whether the type has the specified attribute</summary>
/// <typeparam name="TAttribute">Attribute the type will be checked for</typeparam>
/// <param name="type">
/// Type that will be checked for presence of the specified attribute
/// </param>
/// <returns>True if the type has the specified attribute, otherwise false</returns>
public static bool HasAttribute<TAttribute>(this Type type) {
object[] attributes = type.GetCustomAttributes(typeof(TAttribute), true);
return (attributes != null) && (attributes.Length > 0);
}
}
} // namespace Nuclex.Support