所以我創建此線程:Invoking Private/Protected Methods Via Reflection From The Same Object Instance (or Base)不能反映私人方法
而我們得到了問題修復保存私人方法。因爲這可能不是同一個問題,所以我認爲最好在完整源代碼中發佈不同的問題。這仍然是一項正在進行的工作,但它是功能性的。
的基類:
Public MustInherit Class BaseTransactionalSaveManager : Implements ITransactionalSaveManager
'---- Public Properties & Backing Fields ----'
Public Property FormDataIsValid As Boolean Implements ITransactionalSaveManager.FormDataIsValid
'---- Private Properties & Backing Fields ----'
Protected Property Stages As Collections.Generic.List(Of String)
Protected Property StageCausedRollback As Containers.GenericNamedValuePair(Of String, Boolean)
Protected Property CurrentStage As Integer
'---- Event Declarations & Associated Methods ----'
Public Event TransactionCancelled As EventHandler(Of CustomEventArgs.GenericSingleEventArgs(Of String)) Implements ITransactionalSaveManager.TransactionCancelled
Public Event TransactionCompleted As EventHandler(Of CustomEventArgs.GenericSingleEventArgs(Of String)) Implements ITransactionalSaveManager.TransactionCompleted
Public Event TransactionStagePassed As EventHandler(Of CustomEventArgs.GenericSingleEventArgs(Of String)) Implements ITransactionalSaveManager.TransactionStagePassed
Protected Overridable Sub OnTransactionCancelled(e As CustomEventArgs.GenericSingleEventArgs(Of String)) Implements ITransactionalSaveManager.OnTransactionCancelled
RaiseEvent TransactionCancelled(Me, e)
End Sub
Protected Overridable Sub OnTransactionCompleted(e As CustomEventArgs.GenericSingleEventArgs(Of String)) Implements ITransactionalSaveManager.OnTransactionCompleted
RaiseEvent TransactionCompleted(Me, e)
End Sub
Protected Overridable Sub OnTransactionStagePassed(e As CustomEventArgs.GenericSingleEventArgs(Of String)) Implements ITransactionalSaveManager.OnTransactionStagePassed
RaiseEvent TransactionStagePassed(Me, e)
End Sub
'---- Constructors ----'
Public Sub New()
Stages = New Collections.Generic.List(Of String)
SetStages()
CurrentStage = 0
StageCausedRollback = New Containers.GenericNamedValuePair(Of String, Boolean)
FormDataIsValid = True
End Sub
'---- Public Methods ----'
Public Sub ProcessStage() Implements ITransactionalSaveManager.ProcessStage
' Use stage to fire the correct method.
Me.GetType.InvokeMember(Stages(CurrentStage),
Reflection.BindingFlags.InvokeMethod Or
Reflection.BindingFlags.NonPublic Or
Reflection.BindingFlags.Public Or
Reflection.BindingFlags.Instance,
Type.DefaultBinder, Me, Nothing)
' Determine if the stage should cause a rollback.
If Not StageCausedRollback.Value Then
RollBackTransaction(StageCausedRollback.Name)
Exit Sub
End If
' Check if this stage is the last one.
If Stages(CurrentStage) = Stages.Last Then
OnTransactionCompleted(New CustomEventArgs.GenericSingleEventArgs(Of String)(Stages(CurrentStage)))
Else
OnTransactionStagePassed(New CustomEventArgs.GenericSingleEventArgs(Of String)(Stages(CurrentStage)))
End If
End Sub
Public Overridable Function TryCancelTransaction() As Boolean Implements ITransactionalSaveManager.TryCancelTransaction
OnTransactionCancelled(New CustomEventArgs.GenericSingleEventArgs(Of String)(""))
Return True
End Function
'--- Protected & Overridable Methods ----'
Protected Overridable Sub SetStages()
Me.Stages.Add(MethodNameToString(AddressOf Me.ConfirmFormDataIsValid))
End Sub
Protected Overridable Sub RollBackTransaction(stageThatCauseRollback As String)
OnTransactionCancelled(New CustomEventArgs.GenericSingleEventArgs(Of String)(stageThatCauseRollback))
End Sub
Protected Function MethodNameToString(addressOfMethod As Action) As String
Return addressOfMethod.Method.Name
End Function
Private Sub ConfirmFormDataIsValid()
StageCausedRollback.Name = MethodNameToString(AddressOf ConfirmFormDataIsValid)
StageCausedRollback.Value = If(FormDataIsValid, True, False)
End Sub
End Class
所以這個類是由一個(到目前爲止)空的子類繼承和ProcessStage被調用。注意ConfirmFormDataIsValid()子是私有的。如果你運行它,它不會找到這個方法。如果我將其更改爲受保護,但它工作正常。
我錯過了什麼嗎?
謝謝@vcsjones修復突出顯示...我做錯了什麼? – deanvmc 2012-01-05 16:46:47
回覆:「心理看語法高亮」。語法highlighter使用標籤來決定代碼的編寫語言。由於您擁有C#標籤,它選擇了通過VB.NET突出顯示的C#。您還可以明確地說出該代碼與[HTML評論]的語言(http://meta.stackexchange.com/questions/78363/manually-specify-language-for-syntax-highlighting)。 – vcsjones 2012-01-05 16:46:47
您是否考慮過[Template Method Pattern](http://en.wikipedia.org/wiki/Template_method_pattern)而不是反射?我敢打賭它會更穩定。 (這個評論是基於這個_and_鏈接的問題。) – 2012-01-05 16:47:13