c# - Using Autofac with Dynamic Proxy that output message automatic -


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");     } } 

i using autofac castle dynamicproxy, , try let myclass test method output "begin"/"end" automatic.

public class myloginterceptor : iinterceptor {     public void intercept(iinvocation invocation)     {         console.writeline("begin");         invocation.proceed();         console.writeline("end");     } } 

the following test code:

containerbuilder builder = new containerbuilder(); builder.registertype<mylog>().as<ilog>(); builder.register(c => {     proxygenerator g = new proxygenerator();     object proxy = g.createclassproxy(typeof(myclass), new myloginterceptor());     icanlog proxyicanlog = (icanlog)proxy;     proxyicanlog.log = c.resolve<ilog>();     return proxy; }).as<imyclass>();  using (var container = builder.build()) {     objectcontext.container = container;     imyclass myclass = container.resolve<imyclass>();     myclass.test(); } 

but result no output "begin"/"end", why ?

and if create autologmodule try build log property instance automatic

    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 => property.canwrite && property.propertytype==typeof(ilog));     }      private void injectclassviaproperty(object sender, activatedeventargs<object> evt)     {         var type = evt.instance.gettype();         var propertyinfo = type.getproperties().first(x => x.canwrite && x.propertytype==typeof(ilog));          ilog log = new mylog();         propertyinfo.setvalue(evt.instance, log, null);     } } 

the following test code:

containerbuilder builder = new containerbuilder(); builder.registertype<mylog>().as<ilog>(); builder.registermodule(new autologmodule()); builder.register(c => {     proxygenerator g = new proxygenerator();     object proxy = g.createclassproxy(typeof(myclass), new myloginterceptor());     //icanlog proxyicanlog = (icanlog)proxy;     //proxyicanlog.log = c.resolve<ilog>();     return proxy; }).as<imyclass>();  using (var container = builder.build()) {     objectcontext.container = container;     imyclass myclass = container.resolve<imyclass>();     myclass.test(); } 

the result test method throw "object reference not set instance of object." in log.write("test")

how write feature?


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -