Locking an object to prevent any data changes
<p>Bit of an odd one this...</p>

<p>Lets say I have the following class:</p>

<pre><code>public class Wibble
{
  public string Foo {get;set;}
  public string Bar {get;set;}
}
</code></pre>

<p>This class is used a process where the values of Foo and Bar are updated/changed.  However after a certain point in the process I want to "lock" the instance to prevent any changes from being made.  So the question is how best to do this?</p>

<p>A solution of sorts would be something like this:</p>

<pre><code>public class Wibble
{
  private string _foo;
  private string _bar;

  public bool Locked {get; set;}

  public string Foo
  {
    get
    {
      return this._foo
    }
    set
    {
        if (this.Locked)
        {
          throw new ObjectIsLockedException()
        }

        this._foo = value;
    }
  }

  public string Bar
  {
    get
    {
      return this._bar
    }
    set
    {
        if (this.Locked)
        {
          throw new ObjectIsLockedException()
        }

        this._bar = value;
    }
  }
}
</code></pre>

<p>However this seems a little inelegant.</p>

<p>The reason for wanting to do this is that I have an application framework that uses externally developed plugins that use the class.  The Wibble class is passed into the plugins however some of them should never change the contents, some of them can.  The intention behind this is to catch development integration issues rather than runtime production issues.  Having the object "locked" allows is to quickly identify plugins that are not coded as specified.</p>
