title: Using Autofac with Dynamic Proxy that output message automatic
body: <pre class="lang-cs prettyprint-override"><code>public interface ILog
  {
      void Write(string msg);
  }
  
  public class MyLog : ILog
  {
      public void Write(string msg)
      {
          Console.WriteLine(msg);
      }
  }
  
  public interface ICanLog
  {
      ILog Log { get; set; }
  }
  
  public interface IMyClass
  {
      void Test();
  }
  
  public class MyClass : IMyClass, ICanLog
  {
      public ILog Log { get; set; }
      public void Test()
      {
          Log.Write("Test");
      }
  }
  </code></pre>
  
  <p>I am using <a href="https://autofac.org/" rel="nofollow noreferrer">Autofac</a> with <a href="http://www.castleproject.org/projects/dynamicproxy/" rel="nofollow noreferrer">Castle DynamicProxy</a>,
  and try to let MyClass Test Method output "BEGIN"/"END" automatic.</p>
  
  <pre><code>public class MyLogInterceptor : IInterceptor
  {
      public void Intercept(IInvocation invocation)
      {
          Console.WriteLine("BEGIN");
          invocation.Proceed();
          Console.WriteLine("END");
      }
  }
  </code></pre>
  
  <p>The following is test code:</p>
  
  <pre><code>ContainerBuilder builder = new ContainerBuilder();
  builder.RegisterType&lt;MyLog&gt;().As&lt;ILog&gt;();
  builder.Register(c =&gt;
  {
      ProxyGenerator g = new ProxyGenerator();
      object proxy = g.CreateClassProxy(typeof(MyClass), new MyLogInterceptor());
      ICanLog proxyICanLog = (ICanLog)proxy;
      proxyICanLog.Log = c.Resolve&lt;ILog&gt;();
      return proxy;
  }).As&lt;IMyClass&gt;();
  
  using (var container = builder.Build())
  {
      objectContext.Container = container;
      IMyClass myclass = container.Resolve&lt;IMyClass&gt;();
      myclass.Test();
  }
  </code></pre>
  
  <p>But result no output "BEGIN"/"END", why ?</p>
  
  <p>and if I create AutoLogModule that try build Log Property Instance automatic</p>
  
  <pre><code>    public class AutoLogModule : Autofac.Module
  {
      protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
      {
          var type = registration.Activator.LimitType;
          if (HasPropertyDependencyOnClass(type))
          {
              registration.Activated += InjectClassViaProperty;
          }
      }
  
      private bool HasPropertyDependencyOnClass(Type type)
      {
          return type.GetProperties().Any(property =&gt; property.CanWrite &amp;&amp; property.PropertyType==typeof(ILog));
      }
  
      private void InjectClassViaProperty(object sender, ActivatedEventArgs&lt;object&gt; evt)
      {
          var type = evt.Instance.GetType();
          var propertyInfo = type.GetProperties().First(x =&gt; x.CanWrite &amp;&amp; x.PropertyType==typeof(ILog));
  
          ILog log = new MyLog();
          propertyInfo.SetValue(evt.Instance, log, null);
      }
  }
  </code></pre>
  
  <p>The following is test code:</p>
  
  <pre><code>ContainerBuilder builder = new ContainerBuilder();
  builder.RegisterType&lt;MyLog&gt;().As&lt;ILog&gt;();
  builder.RegisterModule(new AutoLogModule());
  builder.Register(c =&gt;
  {
      ProxyGenerator g = new ProxyGenerator();
      object proxy = g.CreateClassProxy(typeof(MyClass), new MyLogInterceptor());
      //ICanLog proxyICanLog = (ICanLog)proxy;
      //proxyICanLog.Log = c.Resolve&lt;ILog&gt;();
      return proxy;
  }).As&lt;IMyClass&gt;();
  
  using (var container = builder.Build())
  {
      objectContext.Container = container;
      IMyClass myclass = container.Resolve&lt;IMyClass&gt;();
      myclass.Test();
  }
  </code></pre>
  
  <p>The result is Test Method throw
  "Object reference not set to an instance of an object."
  in Log.Write("Test")</p>
  
  <p>How to write this feature?</p>