title: Invoking an Action - determine if the instance it belongs to is null
body: <p>I have a method that takes an Action as a parameter.  Actions are stored in a queue and executed when particular resources become available.  Before I invoke an Action, I'd like to check if the instance it is a member of is null.  </p>
  
  <p>I did a simple test with the following stupid example.  The Action invoked successfully after setting the invokee to null, and as expected, I got a NullReferenceException when attempting to access the property on the null invokee.  Nothing jumped out at me when examining the Action at runtime that suggested I could determine if its instance was null.  </p>
  
  <p>I guess I could pass in the Action and the instance as parameters and test if the instance is null before invoking.  Is it possible to test for a null invokee, or is this just a case of bad design on my part?  </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I added the line, </p>
  
  <p><strong>if (explosion.Target != null)</strong></p>
  
  <p>to Bazooka.Fire(), to check for a null target, but it is still invoking the delegate in my example.  </p>
  
  <pre><code>public void LetsDoThis()
      {
          var bazooka = new Bazooka();
          var rocketLauncher = new RocketLauncher();
  
          bazooka.LockAndLoad(rocketLauncher.BlowStuffUp);
  
          rocketLauncher = null;
  
          bazooka.Fire();
  
          bool wasThisCompletelyAwesome = rocketLauncher.ThisIsAwesome;
      }
  
  public class RocketLauncher
      {
          public void BlowStuffUp()
          {
              bool stuffIsBlowingUp = true;
          }
  
          public bool ThisIsAwesome
          {
              get
              {
                  return true;
              }
          }
      }
  
  public class Bazooka
      {
          private List&lt;Action&gt; explosions = new List&lt;Action&gt;();
  
          public void LockAndLoad(Action loadIt)
          {
              this.explosions.Add(loadIt);
          }
  
          public void Fire()
          {
              foreach (Action explosion in explosions)
                  if (explosion.Target != null)
                      explosion.Invoke();
          }
      }
  </code></pre>