title: C# Unity - change concrete implementation at runtime
body: <p>I'm having a little bit of trouble wrapping my head around IoC - specifically using Unity.</p>
  
  <p>Suppose I have an application that I want to use to send emails. I would model it like this:</p>
  
  <pre><code>public interface IEmailSender
  {
    void SendEmail();
  }
  </code></pre>
  
  <p>And then create some implementations of the interface:</p>
  
  <pre><code>public class GmailEmailSender : IEmailSender
  {
    public void SendEmail()
    {
        //code to send email using Gmail
    }
  }
  
  public class YahooEmailSender : IEmailSender
  {
    public void SendEmail()
    {
        //code to send email using Yahoo
    }
  }
  </code></pre>
  
  <p>I also have a class to actually send the Emails</p>
  
  <pre><code>public class EmailSender
  {
     IEmailSender _emailSender;
     public EmailSender(IEmailSender emailSender)
     {
         _emailSender= emailSender;
     }
  
     public void Send()
     {
        _emailSender.SendEmail();
     }
  }
  </code></pre>
  
  <p>So I understand how to configure Unity to always use one of the implementations:</p>
  
  <pre><code>IUnityContainer container = new UnityContainer().RegisterType&lt;IEmailSender, GmailEmailSender&gt;());
  </code></pre>
  
  <p>but what I'm not quite understanding is if I want to choose the YahooEmailSender based on some criteria and the GmailEmailSender  based on other criteria, where do I code the logic to make that determination, and in turn inject the appropriate concrete implementation to the EmailSender constructor, without using</p>
  
  <pre><code>EmailSender emailSender = new EmailSender(new YahooEmailSender());
  </code></pre>