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 ofIMessageHandler
.
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
最满意答案
我首先要做的是将你的所有* .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 ...
-
HTML中的可滚动堆叠图像(Scrollable Stacked Images in HTML)[2023-12-12]
添加以下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 ... -
递归堆叠的光栅图像(raster images stacked recursively)[2024-02-04]
我首先要做的是将你的所有* .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 ...