Find attribute on class property Since reading Robert Martin's book "Clean Code" I have been inspired to revisit and refactor some of my code to break it down into succinct methods and small specialised classes.

Given the need to retrieve the NameAttribute off the Name2 property of the TestObject class below...

    public class TestObject
    {
        public string Name1 { get; set; }
        [Name("Address")]
        public string Name2 { get; set; }
    }


I have built a class called PropertyNameAttributeFinder to acheive this.

public class PropertyNameAttributeFinder
{
    #region Fields

    private readonly PropertyInfo _property;
    private NameAttribute _attributeFound;

    #endregion

    #region Construtors

    /// &lt;summary&gt;
    /// Initializes a new instance of the &lt;see cref="PropertyNameAttributeFinder"/&gt; class.
    /// &lt;/summary&gt;
    /// &lt;param name="property"&gt;The property.&lt;/param&gt;
    /// &lt;exception cref="System.ArgumentNullException"&gt;property&lt;/exception&gt;
    public PropertyNameAttributeFinder(PropertyInfo property)
    {
        if (property == null) throw new ArgumentNullException("property");

        _property = property;
    }

    #endregion

    #region Public Members

    /// &lt;summary&gt;
    /// Checks for attribute.
    /// &lt;/summary&gt;
    /// &lt;returns&gt;The current instance for fluid API&lt;/returns&gt;
    public PropertyNameAttributeFinder CheckForAttribute()
    {
        SetAttributeIfExists();
        return this;
    }

    /// &lt;summary&gt;
    /// Gets a value indicating whether this instance has found an attribute.
    /// &lt;/summary&gt;
    /// &lt;value&gt;
    /// &lt;c&gt;true&lt;/c&gt; if this instance has found an attribute; otherwise, &lt;c&gt;false&lt;/c&gt;.
    /// &lt;/value&gt;
    public bool HasFoundAttribute
    {
        get { return _attributeFound != null; }
    }

    /// &lt;summary&gt;
    /// Gets the AttributeFound that was found.
    /// &lt;/summary&gt;
    /// &lt;value&gt;
    /// The AttributeFound.
    /// &lt;/value&gt;
    public NameAttribute AttributeFound
    {
        get
        {
            if (!HasFoundAttribute)
            {
                throw new InvalidOperationException("No attribute type was found so cannot be returned. Hint: Use HasFoundAttribute first.");
            }
            return _attributeFound;
        }
    }

    #endregion

    #region Private Members

    private void SetAttributeIfExists()
    {
        _attributeFound = _property.GetAttribute&lt;NameAttribute&gt;();
    }
    #endregion
}


The class will be used as follows...

    [TestMethod]
    public void AttributeFound_WhenCalledAfterCheckAttributeAndPropertyDoesHaveAtrribute_ReturnsInstanceOfAttribute()
    {
        // ARRANGE
        Type testType = typeof(TestObject);
        PropertyInfo property = testType.GetProperty("Name2");

        // ACT
        NameAttribute actual = new PropertyNameAttributeFinder(property)
            .CheckForAttribute()
            .AttributeFound;

        // ASSERT
        Assert.IsNotNull(actual);
    }


Can this class be written better to express it's intent in a clearer manner?

EDIT:
Following request from @Heslacher, here is the code from the extension method called by _property.GetAttribute&lt;NameAttribute&gt;(); in SetAttributeIfExists.

    /// &lt;summary&gt;
    /// Get an attribute for a property
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
    /// &lt;param name="propertyInfo"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static T GetAttribute&lt;T&gt;(this PropertyInfo propertyInfo)
        where T : Attribute
    {
        var attribute = propertyInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault();
        return (T)attribute;
    }


Please note: It is not the code in this extension which I want reviewed, but rather the PropertyNameAttributeFinder class and how calling it reads. I.E. doe the public members clearly reveal the intent of the class.
I think that Heslacher is right and you don't strictly need this class, but as you've written it I'd like to point out a couple of things:


Many people, myself included, don't like to use regions. They just add noise - especially on such a small class
SetAttributeIfExists is one step too far IMO. The method name isn't accurate either, you're setting the value if it exists or not.
The exception message is really odd if you specify a property without the attribute:


[TestMethod]
public void AttributeFound_WhenCalledAfterCheckAttributeAndPropertyDoesntHaveAtrribute_ReturnsNull()
{
    // ARRANGE
    Type testType = typeof(TestObject);
    PropertyInfo property = testType.GetProperty("Name1");

    // ACT
    NameAttribute actual = new PropertyNameAttributeFinder(property)
        .CheckForAttribute()
        .AttributeFound;

    // ASSERT
    Assert.IsNull(actual);
}


I would get an exception saying this:


  No attribute type was found so cannot be returned. Hint: Use HasFoundAttribute first.


That's not a good hint - I've already called HasFoundAttribute. The message could also be more specific as you didn't find a NameAttribute not any old attribute type. The point is you've extended 1 line of code to an entire class for no benefit.