2010-10-28 79 views
0

我試圖自動註冊在團結容器中的所有報告。團結集裝箱類型註冊夸克

所有報告落實iReport的,也有其定義標題,描述和唯一的密鑰(這樣我就可以讀取這些沒有實例的具體類)報表()屬性。

所以......

我得到的報告類型這樣

Public Shared Function GetClassesWhichimplementInterface(Of T)() As IEnumerable(Of Type) 
    Dim InterfaceType = GetType(T) 
    Dim Types As IEnumerable(Of Type) 

    Types = Reflection.Assembly.GetCallingAssembly.GetTypes() 

    Return Types.Where(Function(x) InterfaceType.IsAssignableFrom(x)) 
End Function 

而且這樣註冊它們:

Public Sub RegisterReports() 
    Dim ReportTypes = ReflectionHelper.GetClassesWhichimplementInterface(Of IReport)() 
    For Each ReportType In ReportTypes 
     ''Previously I was reading the report attribute here and using the defined unique key. I've stopped using this code to remove possible problems while debugging. 
     Container.RegisterType(GetType(IReport), ReportType, ReportType.Name) 
    Next 
End Sub 

有通過調用返回到GetClassesWhichimplementInterface()類型和Container.RegisterType()調用沒有錯誤。如果我的電話登記後,立即打電話Container.Resolve(of Interfaces.IReport),我得到以下異常:

Resolution of the dependency failed, type = "MyProject.Interfaces.IReport", name = "(none)". 
Exception occurred while: while resolving. 
Exception is: InvalidOperationException - The current type, MyProject.Interfaces.IReport, is an interface and cannot be constructed. Are you missing a type mapping? 
----------------------------------------------- 
At the time of the exception, the container was: 

    Resolving MyProject.Interfaces.IReport,(none) 

誰能告訴我,爲什麼在容器未保存登記?

回答

1

登記是在容器中。問題在於你在不傳遞命名註冊作爲參數的情況下調用解析。

由於所有的註冊是使用下面的代碼執行:

Container.RegisterType(GetType(IReport), ReportType, ReportType.Name) 

然後所有的人都有一個名字。您必須提供名稱以及類型才能解析容器的依賴關係。

您正在收到的錯誤是因爲沒有名稱沒有註冊的類型映射。

+0

感謝您發現愚蠢的錯誤 – Basic 2010-10-28 14:01:34