title: Custom Attribute to Ensure Encapsulation
body: <p>I'm beginning to look into custom attributes, and I've come up with the following idea: what if I could make an attribute which would restrict the use of a variable to the property it backed?</p>
  
  <pre><code>[RestrictToProperty("Foo")]
  private object _foo;
  public object Foo
  {
      get { return _foo; }
      set
      {
          _foo = value;
          OnFooChanged(EventArgs.Empty);
      }
  }
  public object NotFoo
  {
      get { return _foo; }  // Warning
      set { _foo = value; } // Warning
  }
  public void Bar()
  {
      _foo = new object();  // Warning
  }
  
  // Warning: 'MyClass._foo' should not be used outside of property 'Foo'
  </code></pre>
  
  <p>I believe it's possible, because <code>Obsolete</code> does a similar thing.</p>
  
  <pre><code>[Obsolete]
  private object _foo;
  public void Bar()
  {
      _foo = new object(); // Warning: 'MyClass._foo' is obsolete
  }
  </code></pre>
  
  <p>Unfortunately, I have no idea how to go about it, and can't find much beyond simple runtime attribute tutorials. Is this possible? If so, where would I start?</p>