title: C# Generic T Class TypeOf, is this possible?
body: <p>I have a class that is generic.  <code>Class&lt;T&gt;</code> and depending in the switch statement in the calling code it can be <code>class&lt;int&gt;</code> <code>class&lt;string&gt;</code> <code>class&lt;decimal&gt;</code></p>
  
  <p>The the method that returns this returns it as an object because the calling code has no idea what it is until after it is set.</p>
  
  <p>Is there a way to do this once I get the object back from the function?</p>
  
  <pre><code>load(object result)
  {
      Type t = result.GetType().GetGenericArguments()[0];
  
      Class&lt;t&gt; x = (Class&lt;t&gt;) result;
  }
  </code></pre>
  
  <p>Or do I have to set up an a check to check for each type it can be.  If <code>int</code> then <code>Class&lt;int&gt;</code>, etc...</p>
  
  <p>EDIT:</p>
  
  <p>Here is what I am trying to do, actual code:</p>
  
  <pre><code>public class ReportResult&lt;TP&gt;
  {
      public ReportResult()
      {
          ReportHeaders = new List&lt;ReportHeader&gt;();
          ReportViews = new List&lt;IDataAttributeChild&lt;TP&gt;&gt;();
      }
  
      public List&lt;ReportHeader&gt; ReportHeaders {get;set;}
      public List&lt;IDataAttributeChild&lt;TP&gt;&gt; ReportViews {get;set;}         
  }
  </code></pre>
  
  <p>BAL</p>
  
  <pre><code>    public object GetReportData(ReportProcedureNameEventArg procedureNameEventArg)
      {
          object result = null;
  
          switch (procedureNameEventArg.SelectedNode.Class)
          {
              case ReportClass.Count:
                  var r = new ReportResult&lt;int&gt;
                              {
                                  ReportViews = GetCountByReport(procedureNameEventArg),
                                  ReportHeaders = GetReportHeaders(procedureNameEventArg.SelectedNode.ReportViewId)
                              };
  
                  result = r;
                  break;
              case ReportClass.List:
                  break;
              case ReportClass.Date:
                   var r = new ReportResult&lt;datetime&gt;
                              {
                                  ReportViews = GetDateSummaryReport(procedureNameEventArg),
                                  ReportHeaders = GetReportHeaders(procedureNameEventArg.SelectedNode.ReportViewId)
                              };
  
                  result = r;
                  break;
              default:
                  throw new ArgumentOutOfRangeException();
          }
          return result;
      }
  </code></pre>
  
  <p>The GUI</p>
  
  <pre><code>    public void LoadTreeResult(object result)
      {
          Type t = result.GetType().GetGenericArguments()[0];
  
          ReportResult&lt;?&gt; fff = (ReportResult&lt;?&gt;)result;
  
  
          dgResult.Columns.Clear();
  
          foreach (var header in result.ReportHeaders)
          {
              dgResult.Columns.Add(
                      new DataGridTextColumn
                      {
                          Header = header.Header,
                          Binding = new Binding(header.Binding)
                      });
          }
  
          // This would also be a switch depending on a property coming 
          // back to now what class to cast to in order to populate the grid.
          List&lt;ReportCountByView&gt; d = new List&lt;ReportCountByView&gt;();
  
          foreach (var reportCountByView in result.ReportViews)
          {
              d.Add((ReportCountByView)reportCountByView);
          } 
  
          dgResult.ItemsSource = d;
      }
  </code></pre>
  
  <p>This is a layout of the class model in case it might help.</p>
  
  <p><img src="https://i.stack.imgur.com/4Or27.jpg" alt="enter image description here"></p>
  
  <p><a href="http://i1120.photobucket.com/albums/l489/nitefrog/Reports.jpg" rel="nofollow noreferrer">image of layout</a></p>
  
  <p>Thanks.</p>