title: Calling a function based on parameter type
body: <p>I am trying to figure out how to simplify the following</p>
  
  <p>let's say I have 2 entity classes</p>
  
  <pre><code>public class A
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string City { get; set; }
  }
  </code></pre>
  
  <p>AND </p>
  
  <pre><code>public class B
  {
      public int Id { get; set; } 
      public string Nom { get; set; }
      public string Ville { get; set; }
  } 
  </code></pre>
  
  <p>classes that are similar, but not the same.</p>
  
  <p>each class has a repository classes it uses for CRUD Operations, for example...</p>
  
  <pre><code>public class RepA
  {
      public static List&lt;A&gt; GetAll()
      {
          List&lt;A&gt; list = new List&lt;A&gt;();
  
          A a1 = new A() {Id=1, Name="First A", City="Boston"};
          A a2 = new A() {Id=2, Name="First B", City="Chicago"};
          A a3 = new A() {Id=3, Name="First C", City="San Francisco"};
  
          list.Add(a1);
          list.Add(a2);
          list.Add(a3);
          return list;
      }
  
      public static void SaveAll(List&lt;A&gt; list)
      {
          foreach (A a in list)
          {
                Console.WriteLine("Saved Id = {0} Name = {1} City={2}", 
                    a.Id, a.Name, a.City);
          }
      }
  
  }
  </code></pre>
  
  <p>AND </p>
  
  <pre><code>public class RepB
      {
          public static List&lt;B&gt; GetAll()
          {
              List&lt;B&gt; list = new List&lt;B&gt;();
  
              B b1 = new B() {Id=1, Nom="Second A", Ville="Montreal"};
              B b2 = new B() {Id=2, Nom="Second B", Ville="Paris"};
              B b3 = new B() {Id=3, Nom="Second C", Ville="New Orleans"};
  
              list.Add(b1);
              list.Add(b2);
              list.Add(b3);
              return list;
          }
  
      public static void SaveAll(List&lt;B&gt; list)
      {
          foreach (B b in list)
          {
              Console.WriteLine("Saved Id = {0} Name = {1} City={2}", b.Id, 
                      b.Nom, b.Ville);
          }
      }
  
  }
  </code></pre>
  
  <p>How would I go about making anonymous call to my repository without having to resort to this, because in my real world example, i have 100 repositories, and not 2.</p>
  
  <pre><code>void Main()
  {
      ChosenType chosentype    = RandomChosenType(); //A or B
      switch (chosentype)
      {
          case ChosenType.A:
              var listA = RepA.GetAll();
              RepA.SaveAll(listA);
              break;
          case ChosenType.B:
              var listB = RepB.GetAll();
              RepB.SaveAll(listB);
              break;
              default:
              break;
      }
  }
  </code></pre>