title: C# : Passing a Generic Object
body: <p>I want to have a generic print function...PrintGeneric(T)...in the following case, what am I missing?</p>
  
  <p>As always your help/insight is appreciated...</p>
  
  <pre><code>public interface ITest
  {}
  
  public class MyClass1 : ITest
  {
      public string myvar = "hello 1";
  }
  
  public class MyClass2 : ITest
  {
      public string myvar = "hello 2";
  }
  
  class DoSomethingClass
  {
  
      static void Main()
      {
          MyClass1 test1 = new MyClass1();
          MyClass2 test2 = new MyClass2();
  
          Console.WriteLine(test1.myvar);
          Console.WriteLine(test2.myvar);             
          Console.WriteLine(test1.GetType());
  
          PrintGeneric(test1);
          PrintGeneric&lt;test2.GetType()&gt;(test2);
      }
  
      // following doesn't compile
      public void PrintGeneric&lt;T&gt;(T test)
      {
          Console.WriteLine("Generic : " + test.myvar);
      }
  }
  </code></pre>