title: Creating an Instance of an Interface
body: <p>I have the following interfaces defined:</p>
  <pre><code>public interface IAudit {
      DateTime DateCreated { get; set; }
  }
  
  public interface IAuditable {
      IAudit Audit { get; set; }
  }
  </code></pre>
  <p>The <code>IAuditable</code> interface says which classes I will have an <code>Audit</code> for. The <code>IAudit</code> interface is the actual <code>Audit</code> for that class. For example say I have the following implementations:</p>
  <pre><code>public class User : IAuditable {
      public string UserName { get; set; }
      public UserAudit Audit { get; set; }
  }
  
  public class UserAudit : IAudit {
      public string UserName { get; set; }
      public DateTime DateCreated { get; set; }
  
      public UserAdit(User user) {
          UserName = user.UserName;
      }
  }
  </code></pre>
  <p>Now given an object which is <code>IAuditable</code> (<code>User</code> from above), I'd like to be able to create an intance of <code>IAudit</code> (<code>UserAdit</code> from above) by feeding in the <code>IAuditable</code> object into the constructor. Ideally i'd have something like:</p>
  <pre><code>if (myObject is IAuditable) {
      var audit = new IAudit(myObject) { DateCreated = DateTime.UtcNow }; // This would create a UserAudit using the above example
  }
  </code></pre>
  <p>However I have a bunch of problems:</p>
  <ul>
  <li>You can't create an instance of an interface</li>
  <li>No where in the code does it define which <code>IAudit</code> applies to which <code>IAuditable</code></li>
  <li>I can't specify that the <code>IAudit</code> interface must have a constructor which takes an <code>IAuditable</code></li>
  </ul>
  <p>I'm sure this is a design pattern many have had before but I can't get my head around it. I'd really appreciate if someone could show me how this can be achieved.</p>