title: In C#, can we add methods into a public Behaviour class without touching it?
body: <p>In <em>Unity csharp</em>, I want to make a <code>GetOrAddComponent</code> method, which will simplify the respectives <code>GetComponent</code> and <code>AddComponent</code> (for no good reason I suppose).</p>
  
  <p>The usual way is this:</p>
  
  <pre><code>// this is just for illustrating a context
  using UnityEngine;
  class whatever : MonoBehavior {
  public Transform child;
  void whateverMethod () {
  
      BoxCollider boxCollider = child.GetComponent&lt;BoxCollider&gt;();
      if (boxCollider == null) {
          boxCollider = child.gameObject.AddComponent&lt;BoxCollider&gt;();
      }
  
  }}
  </code></pre>
  
  <p>Right now I could make this class . . . :</p>
  
  <pre><code>public class MyMonoBehaviour : MonoBehaviour {
  
      static public Component GetOrAddComponent (Transform child, System.Type type) {
          Component result = child.GetComponent(type);
          if (result == null) {
              result = child.gameObject.AddComponent(type);
          }
          return result;
      }
  
  }
  </code></pre>
  
  <p>. . . So this works:</p>
  
  <pre><code>// class whatever : MyMonoBehavior {
  
  BoxCollider boxCollider = GetOrAddComponent(child, typeof(BoxCollider)) as BoxCollider;
  </code></pre>
  
  <p>But I wish I could write it like this:</p>
  
  <pre><code>BoxCollider boxCollider = child.GetOrAddComponent&lt;BoxCollider&gt;();
  </code></pre>
  
  <p>The only idea I could come up with would be way too complicated to do it (replacing each <code>Transform</code> with a <code>MyTransform</code>) and thus not worth the trouble of even trying. At least not just for a nicer syntax.</p>
  
  <p>But is it? Or is there any other way this could be achieved?</p>