首页 \ 问答 \ Autofac:实现非注入的非泛型接口的泛型类(Autofac: generic class implementing a non generic interface not being injected)

Autofac:实现非注入的非泛型接口的泛型类(Autofac: generic class implementing a non generic interface not being injected)

假设我有以下(简化)对象模型:

public abstract class Message
{
}
public class SimpleMessage:Message
{
}
public class ComplexMessage:Message
{
}
public interface IMessageHandler{

    void Handle(Message message);
}

public class NonGenericMessageHandler1: IMessageHandler{

    void Handle(Message message){      
      Console.WriteLine("hi dad!")
    }
}   
public class NonGenericMessageHandler2: IMessageHandler{
    void Handle(Message message){        
      Console.WriteLine("hi mom!")
    }
}
public class GenericMessageHandler<T> :IMessageHandler where T: Message>{ 
    void Handle(Message message){
        //do something cool with the generic
    }
}   

 public class MessageHandlerFactory: IMessageHandlerFactory{
    public MessageHandlerFactory(IEnumerable<IMessageHandler> handlers){
    }
    public IMessageHandler Create(Message message){
         //return the right handler
    }   
 }

这是问题所在。 注入MessageHandlerFactory的IEnumerable<IMessageHandler> autofac仅包含IMessageHandler的非通用实现。
如果我想要通用版本,我必须手动声明各种封闭版本,如下所示:

 public MessageHandlerFactory(IEnumerable<IMessageHandler> handlers, MessageHandler<SimpleMessage> handler1, MessageHandler<ComplexMessage> handler2){

 }

这是我如何连接Autofac:

  builder.RegisterAssemblyTypes(typeof (IMessageHandlerFactory).Assembly)
                 .AsImplementedInterfaces()
                 .AsSelf()
                 .SingleInstance();

          builder.RegisterGeneric(typeof (MessageHandler<>)).AsSelf().SingleInstance();

如何让Autofac传递所有IMessageHandler实现者的统一集合,包括任何通用变体?


Say I have the following (simplified) object model:

public abstract class Message
{
}
public class SimpleMessage:Message
{
}
public class ComplexMessage:Message
{
}
public interface IMessageHandler{

    void Handle(Message message);
}

public class NonGenericMessageHandler1: IMessageHandler{

    void Handle(Message message){      
      Console.WriteLine("hi dad!")
    }
}   
public class NonGenericMessageHandler2: IMessageHandler{
    void Handle(Message message){        
      Console.WriteLine("hi mom!")
    }
}
public class GenericMessageHandler<T> :IMessageHandler where T: Message>{ 
    void Handle(Message message){
        //do something cool with the generic
    }
}   

 public class MessageHandlerFactory: IMessageHandlerFactory{
    public MessageHandlerFactory(IEnumerable<IMessageHandler> handlers){
    }
    public IMessageHandler Create(Message message){
         //return the right handler
    }   
 }

Here's the problem. The IEnumerable<IMessageHandler> autofac injects into MessageHandlerFactory only contains the nongeneric implementations of IMessageHandler.
If I want the generic version, I have to declare the various closed versions manually, as follows:

 public MessageHandlerFactory(IEnumerable<IMessageHandler> handlers, MessageHandler<SimpleMessage> handler1, MessageHandler<ComplexMessage> handler2){

 }

Here's how I'm wiring up Autofac:

  builder.RegisterAssemblyTypes(typeof (IMessageHandlerFactory).Assembly)
                 .AsImplementedInterfaces()
                 .AsSelf()
                 .SingleInstance();

          builder.RegisterGeneric(typeof (MessageHandler<>)).AsSelf().SingleInstance();

How can I get Autofac to pass in a unified collection of all IMessageHandler implementors, including any generic variants?


原文:https://stackoverflow.com/questions/18727197
更新时间:2023-10-21 10:10

最满意答案

我首先要做的是将你的所有* .tiff放在一个文件夹中。 然后将所有名称读入列表。 堆叠它们然后写一个多层栅格。 我假设所有图像具有相同的范围和投影。

    ### Load necessary packages
library(tiff)
library(raster)
library(sp)
library(rgdal)   #I cant recall what packages you might need so this is probably 
library(grid)    # overkill
library(car)

############ function extracts the last n characters from a string  
############ without counting the last m
subs <- function(x, n=1,m=0){
  substr(x, nchar(x)-n-m+1, nchar(x)-m)
  }



setwd("your working directory path") # you set your wd to were all your images are
filez <- list.files() # creates a list with all the files in the wd
no <- length(filez) # amount of files found
imagestack <- stack() # you initialize your raster stack

for (i in 1:no){

  if (subs(filez[i],4)=="tiff"){

  image <- raster(filez[i]) # fill up raster stack with only the tiffs

  imagestack <- addLayer(imagestack,image)
   }
}

writeRaster(imagestack,filename="output path",options="INTERLEAVE=BAND",overwrite=TRUE)
# write stack

我没试过这个,但它应该有效。


What I would first do is put all your *.tiff in a single folder. Then read all their names into a list. Stack them and then write a multi-layered raster. I'm assuming all the images have the same extent and projection.

    ### Load necessary packages
library(tiff)
library(raster)
library(sp)
library(rgdal)   #I cant recall what packages you might need so this is probably 
library(grid)    # overkill
library(car)

############ function extracts the last n characters from a string  
############ without counting the last m
subs <- function(x, n=1,m=0){
  substr(x, nchar(x)-n-m+1, nchar(x)-m)
  }



setwd("your working directory path") # you set your wd to were all your images are
filez <- list.files() # creates a list with all the files in the wd
no <- length(filez) # amount of files found
imagestack <- stack() # you initialize your raster stack

for (i in 1:no){

  if (subs(filez[i],4)=="tiff"){

  image <- raster(filez[i]) # fill up raster stack with only the tiffs

  imagestack <- addLayer(imagestack,image)
   }
}

writeRaster(imagestack,filename="output path",options="INTERLEAVE=BAND",overwrite=TRUE)
# write stack

I did not try this, but it should work.

相关问答

更多
  • 我觉得你忘记在追加之前从容器中删除所有图像。 这样做,它应该工作。请告诉我它是否解决了你的问题 var myNode = document.getElementById("foo"); while (myNode.firstChild) { myNode.removeChild(myNode.firstChild); } 您还可以检查此问题在JavaScript中删除DOM节点的所有子元素 i feels You have forgotten to remove all the images from t ...
  • 添加以下css #imageStack { position: relative; } img { position: absolute; } img:nth-child(1) { top: 0; left: 0; } img:nth-child(2) { top: 6px; left: 8px; } .... etc 它们将相对于imageStack元素显示 add the following css #imageStack { posi ...
  • 我首先要做的是将你的所有* .tiff放在一个文件夹中。 然后将所有名称读入列表。 堆叠它们然后写一个多层栅格。 我假设所有图像具有相同的范围和投影。 ### Load necessary packages library(tiff) library(raster) library(sp) library(rgdal) #I cant recall what packages you might need so this is probably library(grid) # overk ...
  • 这对我来说有点混乱,我想我仍然无法完全理解你的问题。 但那是你在找什么? http://jsfiddle.net/ALkKB/15/ @media screen and (max-width: 768px) { #iframe{width:100%; height:auto;} } I appreciate all of your help San. I ended up eliminating the use of Bootstrap and just implemented my own CSS ...
  • 使用display : inline-block为d1, d2 d3和d4 (以及vertical-align )并在父对象上使用text-align: center 这些元素不需要相对定位 use display : inline-block for d1, d2 d3 and d4 (along with a vertical-align) and use text-align: center on the parent Relative positioning is not necessary for ...
  • 一种可能的解决方案是为每个子图像创建自定义SVG叠加。 在onmouseover事件中,您将降低SVG元素的不透明度,使下面的图像更加明显。 签出此工具以创建SVG。 Abandoned the project, the ony solution is SVG but it nos possible at this stage as some browsers/OS do not support fully them
  • 首先,我没有看到你在你的页面中包含了jquery.js,砌体是如何工作的? 第二,只在你的页面上留下这个 $(window).load(function(){ var $container = $('#grid'); $container.imagesLoaded( function() { $container.masonry({itemSelector: '.grid-item'}); }); }); 所有其他砖石电话都是不必要的。 First, i don't ...
  • 如果您的栅格都在同一个文件夹中,则不需要for循环来执行此操作: import arcpy wd="Y:/" #have this as your directory where all rasters are located arcpy.env.workspace = wd raster_list=arcpy.ListRasters("", "tif") arcpy.CompositeBands_management(raster_list,"stacked_img.tif") #will save ou ...
  • 问题 你有一个错字: #wisdomCotainer ,应该是#wisdomContainer 笔记 不要使用HTML标签width / height ,而是使用CSS 标签是自闭式标签,因此不需要 在这种情况下,您可以使用直接子选择器>来简化CSS #containerContainer { position: relative; } #containerContainer > div { position: absolute; height: 744px; ...
  • 尝试在θ角度中添加边距,以便在最后一个图像的theta-length结束时,下一个θ-start不会立即开始。 这是15度的利润: Image 0: theta-start = 0 theta-length = 30 Image 1: theta-start = 45 theta-length = 30 Image 2: theta-start = 90 theta-length = 30 Image 3: theta-start = 135 theta-length = 30 Image 4: theta ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。