title: Accessing instance-level custom attributes in c#
body: <p>Imagine I have the following code:</p>
  
  <pre><code>class A
  {
      [UsefulAttribute("foo")]
      B var1;
      [UsefulAttribute("bar")]
      B var2;
  
      ...
  }
  
  
  class B
  {
      public string WriteSomethingUseful()
      {
          ?????
      }
  }  
  </code></pre>
  
  <p>My question is, what code do it need to put in the <strong>?????</strong> such that, when I call <strong>var1.WriteSomethingUseful</strong> I get an output of <strong>foo</strong>, and when I call <strong>var2.WriteSomethingUseful</strong> I get an output of <strong>bar</strong>?</p>
  
  <p>I've got a feeling this is quite a straightforward question, I think my main issue is that I have worked myself into a state of confusion by thinking about it for too long!!!</p>
  
  <p>Seriously, I have defined <strong>UsefulAttribute</strong> and realise that part of the code must be a <strong>GetCustomAttributes(typeof(UsefulAttribute)...)</strong> call. Where I'm getting confused is how to pull these values out on the actual instance, rather than at the type level.</p>
  
  <p>Many thanks,
  Pete</p>