private void ComponentContainer_NoComponentRegisted(object sender, NoComponentRegistedEventArgs e) { if (!e.ServiceType.IsInterface) { return; } IEnumerable <AttachedRuntimeInfo> matchedInfos = this.attachedImplementorInfo .Where(info => info.Attachment.CanAttach(e.ServiceType)); int count = matchedInfos.Count(); if (count == 0) { return; } if (count > 1) { throw new ApplicationException(); } var implementorType = matchedInfos.First().AttachedType; e.ComponentProvider = cm => { var implementor = (IImplementor)cm.CreateComponent(implementorType); ImplementorAttributeExecuteInterceptor interceptor = new ImplementorAttributeExecuteInterceptor(e.ServiceType, implementor); var rlt = proxyGenerator.CreateInterfaceProxyWithoutTarget( e.ServiceType, interceptor ); return(rlt); }; }
private void AutofacContainerBuilder_Building(object sender, AppContainerBuilderBuildEventArgs e) { ProxyGenerator proxyGenerator = new ProxyGenerator(); AutofacContainerBuilder autofacContainerBuilder = (AutofacContainerBuilder)sender; foreach (var info in attributeAndTypeInfos) { autofacContainerBuilder.RegisterByCreator(c => { c.InjectProperties(info.Attribute); ImplementorAttributeExecuteInterceptor interceptor = new ImplementorAttributeExecuteInterceptor(info.Type, (ImplementorAttribute)info.Attribute); var rlt = proxyGenerator.CreateInterfaceProxyWithoutTarget( info.Type, interceptor ); return(rlt); }, info.Type); } }