title: C# - Pass a variable type to Generic method
body: <p>I have this code using NPOI.<br>
  I'm trying to have an object that will be a either a<code>HSSFWorkbook</code> or a <code>XSSFWorkbook</code> depending on version of excel file.</p>
  
  <p>It is possible to have an <code>Type</code> and past it to generic method in order to cast then return either <code>HSSFWorkbook</code> or <code>XSSFWorkbook</code>?  </p>
  
  <p>And also if it works, I will use <code>GetWorkBook()</code> in different method.  </p>
  
  <p>Please see my comment on constructor.  </p>
  
  <pre><code>public class ExcelReader
  {    
      public ExcelReader(filePath)
      {
          var isXls = Path.GetExtension(_filePath) == ".xls";
          // Is the following possible or is there any work around to get it work.
          var type = isXls ? HSSFWorkbook : XSSFWorkbook; 
          var workbook = GetWorkBook&lt;type&gt;();
          // Other init...
      }
  
      public T GetWorkBook&lt;T&gt;()
      {        
          return (T)Workbook.GetSheetAt();
      }
  }
  </code></pre>