Every once in a while, we have no choice but to write “dangerous” code. The type of code that, when used improperly, can wreak all sorts of havoc on the system. Purging audit trail records, fast-swapping statuses, or, like in today’s example, code that toggles an object’s "IsUpdated" property regardless of whether that object has been updated.

It’s a good idea to protect such code, perhaps using access specifiers like “private” or “internal” or even runtime-enforced security like the System.Security.CodeAccessPermission namespace. Or, one could go the route of Riaan Hanekom's predecessor and use some sort of… password protection?

/// <summary>
/// Overrides the IsUpdated value. Used to set the value to false 
/// after properties have been populated by the constructor.
/// </summary>
/// <param name="Password">
/// The password used to verify authorization to execute the method.
/// </param>
/// <param name="status">The new value for IsUpdated.</param>

public void OverrideIsUpdated(String password, bool status) 
{
    if (password != "P#$$word#") 
    {
        throw new ArgumentException("Invalid password specified!"
          + " This method is for internal use only!", "Password");
    }

    _IsUpdated = status;

   ... snip ...
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!