title: How do you cast a method argument to a generic without defining the generic's type in c#
body: <p>First of all the following problem is with a Xamarin app I'm writing but I assume this happens because of C# not because of the underlying Java, if it is because of Java then please tell me so I can correct the question title</p>
  
  <p>I wish to create an abstract class that I will use for the basis of all Fragment classes my project will use. So I have defined it like this:</p>
  
  <pre><code>public abstract class CoreFragment&lt;T&gt; : Fragment
  </code></pre>
  
  <p>The T in the class represents an interface that must be implemented by whichever class this Fragment attaches to, so for example my code is:</p>
  
  <pre><code>private T _parent;
  
  public override void OnAttach(Activity activity){
      if (activity is T){
          _parent = activity as T;
      } else { throw new Exception("Incorrect type of parent")' }
  }
  </code></pre>
  
  <p>So when I create a new fragment that should be attached only to a type that implements the interface iMainInteractions, I create it as :</p>
  
  <pre><code>public class MyNewFragment : CoreFragment&lt;iMainInteractions&gt;
  </code></pre>
  
  <p>my problem is that the code above:</p>
  
  <pre><code>_parent = activity as T;
  </code></pre>
  
  <p>does not compile, I even tried:</p>
  
  <pre><code>_parent = (T) activity;
  </code></pre>
  
  <p>because it sometimes seems to work where as 'as' doesn't and it keeps throwing a compiler error that T is not specified to be of a specific type (I assume it wants me to define something along the lines of where T : something)</p>
  
  <p>if I remove it and use something like :</p>
  
  <pre><code> _parent = (T) Convert.ChangeType(activity, typeof(T));
  </code></pre>
  
  <p>it will compile, it will enter the true part of the statement when the time comes but when the ChangeType tries to execute, it will throw an exception saying that activity is not implementing IConvertible</p>
  
  <p>so other than creating a blank iInteraction interface as a parent of T, and having all my interaction interfaces follow this one, is there a way to make this work?</p>
  
  <p>thanks in advance for any help you can provide</p>