title: C# function to return the implemented interface of a type
body: <p>I have this singleton factory class example, where I used a dictionary and determine the relevant report class at runtime.. because there are up to 40 combinations based on what each report service also has to do.</p>
  
  <pre><code>public sealed class ReportServiceFactory : IGetReportServices
  {
      private static readonly ReportServiceFactory instance = new ReportServiceFactory();
  
      private static readonly Dictionary&lt;Tuple&lt;ReportTypes, ReportPeriodTypes&gt;, IServiceReports&gt; reportEntityServices =
          new Dictionary&lt;Tuple&lt;ReportTypes, ReportPeriodTypes&gt;, IServiceReports&gt; {
              { Tuple.Create(ReportTypes.Foo, ReportPeriodTypes.Week), new FooService&lt;FooWeekEntity&gt;() },
              { Tuple.Create(ReportTypes.Foo, ReportPeriodTypes.Month), new FooService&lt;FooMonthEntity&gt;() },
  
              { Tuple.Create(ReportTypes.Bar, ReportPeriodTypes.Week), new BarService&lt;BarWeekEntity&gt;() },
              { Tuple.Create(ReportTypes.Bar, ReportPeriodTypes.Month), new BarService&lt;BarMonthEntity&gt;() }
          };
  
      // Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
      static ReportServiceFactory()
      {
      }
  
      private ReportServiceFactory()
      {
      }
  
      public static ReportServiceFactory Instance
      {
          get { return instance; }
      }
  
      public IServiceReports Get(ReportTypes reportType, ReportPeriodTypes periodType)
      {
          if (!Enum.IsDefined(typeof(ReportTypes), reportType))
              throw new ArgumentOutOfRangeException("reportType");
          if (!Enum.IsDefined(typeof(ReportPeriodTypes), periodType))
              throw new ArgumentOutOfRangeException("periodType");
  
          var reportTuple = Tuple.Create(reportType, periodType);
  
          if (!reportEntityServices.ContainsKey(reportTuple))
              return null;
  
          return reportEntityServices[reportTuple];
      }
  }
  </code></pre>
  
  <p>I want to be able to return a more specific interface for the <code>Get()</code> function on the factory, to change it to <code>IServiceFooReports</code> at execution time as an example... this is so I have access to the below <code>FooViewModel</code> <code>Get()</code> function <strong>without casting the result in other calling code.</strong></p>
  
  <pre><code>public interface IServiceFooReports : IServiceReports
  {
      IEnumerable&lt;FooViewModel&gt; Get();
  }
  
  public class FooService&lt;T&gt; : IServiceFooReports
  {
        ... use EF model context and call .CreateObjectContext&lt;T&gt;()
        do filtering on the IQueryable result and return the result materialised as the relevant view models
  }
  </code></pre>
  
  <p>Is this possible? here is the <code>IGetReportServices</code> interface in case that also needs changing. <code>IServiceReports</code> is empty and currently only being used as a way to store these report services in the same dictionary... a marker interface? </p>
  
  <pre><code>public interface IGetReportServices
  {
      IServiceReports Get(ReportTypes reportType, ReportPeriodTypes reportPeriodType);
  }
  </code></pre>