title: How to fire an extension method by passing a generic type to a base class?
body: <p><strong>I want to know how to get access to an extension method from within a base class by passing the generic type.</strong></p>
  
  <p>I have several different classes all containing ToModel and ToContract Funcs.
  These methods are to switch from Entity Framework type to Data Contracts and vice versa. </p>
  
  <p>They all do the same repetitive calls so I would like to condense down the code.</p>
  
  <p>I have tried invoking functions using reflection. And many other methods to none avail. I have simplified the problem that I am facing in the following code.</p>
  
  <p>My problem is that I cannot access the extension method from within the base class. Please help.</p>
  
  <p>Error I am receiving: Class Does not contain a definition for 'ToModel' method.</p>
  
  <h2>Simplified Code</h2>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace TestExtensions
  {    
      public class Program : Base&lt;Client&gt;
      {
          static void Main(string[] args)
          {
              Client c = new Client();
              c.FirstName = "First Name";
  
              Console.WriteLine(c.FirstName);
  
              c.ToModel();
  
              Console.WriteLine(c.FirstName);
  
              Program p = new Program();
              p.go(c);
          }
  
          public void go(Client c)
          {
              base.ChangeNameAgain(c);
              Console.WriteLine(c.FirstName);
  
          }
      }
  }
  </code></pre>
  
  <p><strong>Extension Class</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace TestExtensions
  {
      public static class ExtensionClass
      {
          public static Client ToModel(this Client c)
          {
              c.FirstName = "First Name Changed";
  
              return c;
          }
      }
  }
  </code></pre>
  
  <p><strong>Client Class</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace TestExtensions
  {
      public class Client
      {
          public string FirstName { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>Base Class</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace TestExtensions
  {
      public class Base&lt;T&gt;
      {
          public T ChangeNameAgain(T c)
          {
              // This is where I need help
              // Need to invoke the ToModel Method using reflection               
  
  
              return (T) c;
          }
  
      }
  }
  </code></pre>
  
  <h2>My actual code</h2>
  
  <p><strong>Base</strong></p>
  
  <pre><code>public class BaseApi&lt;TContractType, TModelType&gt; : ApiController
      where TModelType : DataModelBase, IApiExtensionModel, new()
      where TContractType: DataContractBase, IApiExtensionContract, new()
  {
  
   public TContractType Create(
          IGenericRepository&lt;TModelType&gt; repo, 
          TContractType obj)        
      {                        I   
                  // This is what is giving me trouble               
                  var instance = (TContractType)
                      Activator.CreateInstance(typeof(TContractType), 
                      new object[] { obj }); 
  
                  var modelMap = instance.ToModel(); 
                  var ret = (dynamic)repo.Edit(modelMap);                    
                  return ret.ToContract();
      } 
  
  }
  </code></pre>
  
  <h2><strong>Code that actually works</strong></h2>
  
  <p>Trying to make it generic as it is repetitive.</p>
  
  <pre><code>    [Microsoft.AspNetCore.Mvc.HttpPost]
      public patientContract.Patient CreatePatient(
          [Microsoft.AspNetCore.Mvc.FromBody] patientContract.Patient patient)
      {
  
              var map = patient.ToModel();
              var ret = _patientRepo.Add(map);
              return ret.ToContract();
      }
  </code></pre>
  
  <p><strong>Extensions</strong></p>
  
  <pre><code>public static class PatientContractExtension
  {
      public static Model.Patient ToModel(this Contract.Patient patientContract)
      {  
          var map = Mapper.Map&lt;Contract.Patient, Model.Patient&gt;(patientContract);
  
          return map; 
      }
  
      public static Contract.Patient ToContract(this Model.Patient patientModel)
      {
          var map = Mapper.Map&lt;Model.Patient, Contract.Patient&gt;(patientModel);
  
          return map;
      }                    
  </code></pre>
  
  <h2>    }</h2>