我試圖用統一的容器在vb.net依賴注入vb.net使用統一
當我這樣做調用resolve方法,我收到了resolutionFaieldException錯誤
依賴的解決方案未能實現依賴注入,type =「Test.EmailSending」,name =「PDFEmailSender」。 發生異常時:解析時。
Module Module1
Private iemail As IErrorEmail
Sub Main()
'Dim email As New Test.EmailSending(iemail)
'email.Email()
Dim unity As UnityContainer = New UnityContainer
unity.RegisterType(Of IErrorEmail, SQLErrorEmail)("SQLErrorEmail")
unity.RegisterType(Of IErrorEmail, GenericEmail)("GenericEmail")
unity.RegisterType(Of IErrorEmail, Emailsending)("Emailsending")
Dim email1 = unity.Resolve(Of EmailSending)("SQLErrorEmail")
email1.Email()
End Sub
End Module
界面簡單錯誤電子郵件接口
Namespace InterfaceError
Public Interface IErrorEmail
Function ErrorEmail() As Boolean
End Interface
End Namespace
我已經添加了兩個類型的電子郵件類別的其中實現I誤差接口
Imports InterfaceSender.InterfaceError
Public Class SQLErrorEmail
Implements IErrorEmail
Private _SenderEmail As IErrorEmail
Public Sub New(SQLErrorEmail As IErrorEmail)
_SenderEmail = SQLErrorEmail
End Sub
Public Function ErrorEmail() As Boolean Implements IErrorEmail.ErrorEmail
Dim isemailed As Boolean = False
Try
Email()
Catch ex As Exception
End Try
Return isemailed
End Function
Public Shared Sub Email()
Console.WriteLine("SQL Error Email")
Console.ReadLine()
End Sub
End Class
上述SQL錯誤類實現ErrorEmail函數並向控制檯寫入它是SQL錯誤電子郵件。
Imports InterfaceSender.InterfaceError
Public Class GenericEmail
Implements IerrorEmail
Private _SenderEmail As IErrorEmail
Public Sub New(GenericEmail As IErrorEmail)
_SenderEmail = GenericEmail
End Sub
Public Function SendEmail() As Boolean Implements IErrorEmail.ErrorEmail
Dim isemailed As Boolean = False
Try
Email()
Catch ex As Exception
End Try
Return isemailed
End Function
Public Shared Sub Email()
Console.WriteLine("Generic Error Email")
Console.ReadLine()
End Sub
End Class
上述類實現繼承了I誤差接口和當電子郵件方法被調用它的一個輸出Genric電子郵件輸出到控制檯的一般錯誤。
Imports Method.GenericEmail
Imports Method.SQLErrorEmail
Imports InterfaceSender.InterfaceError
Public Class EmailSending
Private _Isender As IErrorEmail
Public Sub New(ByVal Email As IErrorEmail)
MyBase.New()
_Isender = Email
End Sub
Public Sub EmailSendingLogic()
Dim type As String = "SQL"
If type = "SQL" Then
Method.SQLErrorEmail.Email()
ElseIf type = "Generic" Then
Method.GenericEmail.Email()
End If
End Sub
End Class
你也註冊了EmailSending嗎?因爲它沒有在 –
以上的代碼中註冊不,我不會嘗試 – user1339913
您嘗試創建的對象圖是什麼?你想在EmailSending中注入哪個'IErrorEmail'實現?你能解釋一下你想做什麼嗎? –