title: How to get rid of an explicit cast in a Chain of Responsibility?
body: <p>I'm currently have an implementation of Chain of Responsibility which return objects that implement IResponse.</p>
  
  <pre><code>public interface IRequest
  {
  }
  
  public interface IResponse
  {
  }
  
  public interface IFactory
  {
      bool CanHandle(IRequest request);
      IResponse HandleRequest(IRequest request);
  }
  
  public class Foo : IResponse
  {
      public void SpecificMethod()
      {
          Console.WriteLine("SpecificMethod() only belongs to Foo");
      }
  }
  
  public class FooRequest : IRequest
  {
  }
  
  public class FooFactory : IFactory
  {
      public bool CanHandle(IRequest request)
      {
          return request is FooRequest;
      }
  
      public IResponse HandleRequest(IRequest request)
      {
          return new Foo();
      }
  }
  
  public class FactoryManager
  {
      private readonly List&lt;IFactory&gt; _factoryImplementations = new List&lt;IFactory&gt;();
  
      public void Register(IFactory factory)
      {
          _factoryImplementations.Add(factory);
      }
  
      public IResponse HandleRequest(IRequest request)
      {
          foreach (var factory in _factoryImplementations)
          {
              if (factory.CanHandle(request))
              {
                  return factory.HandleRequest(request);
              }
          }
          return null;
      }
  }
  
  
  
  class Program
  {
      static void Main()
      {
          var manager = new FactoryManager();
          manager.Register(new FooFactory());
          var foo = (Foo) manager.HandleRequest(new FooRequest()); // How can I remove this cast?
          foo.SpecificMethod();
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>The purpose of this implementation is to make it easy to replace implementations whenever I need. The problem is that I have to explicitly cast the type which I made the request for if I want to do anything specific with the object, like accessing <code>foo.SpecificMethod()</code>.</p>
  
  <p>Is there any way to have this <code>(Foo)</code> cast gone?</p>
  
  <p>Edit: It's possible to solve this issue with a dynamic variable, but a statically typed way of solving it would be preferrable.</p>