Imports System.Reflection
Namespace SimpleHistory
Public Class ObjectAction
Inherits ObjectBase
Public Property [Object] As Object = Nothing
Public ReadOnly Property ParametersUndo As New List(Of Object)
Public ReadOnly Property ParametersRedo As New List(Of Object)
Public Property MethodUndo As MethodInfo = Nothing
Public Property MethodRedo As MethodInfo = Nothing
Public Property AutogenerateObject As Boolean = True
'''
''' Creates a new Instance of Object Action.
'''
Public Sub New()
End Sub
Private Function GetMethodInfo(obj As Object, name As String, flags As BindingFlags)
Return obj.GetType.GetMethod(name, flags)
End Function
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The name of the methode to call on Undo.
''' The name of the methode to call on Redo.
Public Sub New(obj As Object, methodNameUndo As String, methodNameRedo As String)
Me.New(obj, methodNameUndo, methodNameRedo, {}, {}, BindingFlags.Default, BindingFlags.Default)
End Sub
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The name of the methode to call on Undo.
''' The name of the methode to call on Redo.
''' The parameters for calling the methode on Undo.
''' The parameters for calling the methode on Redo.
Public Sub New(obj As Object, methodNameUndo As String, methodNameRedo As String, paramsUndo As Object(), paramsRedo As Object())
Me.New(obj, methodNameUndo, methodNameRedo, paramsUndo, paramsRedo, BindingFlags.Default, BindingFlags.Default)
End Sub
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The name of the methode to call on Undo.
''' The name of the methode to call on Redo.
''' The parameters for calling the methode on Undo.
''' The parameters for calling the methode on Redo.
''' The Binding Flags of Methode on Undo.
''' The Binding Flags of Methode on Redo.
Public Sub New(obj As Object, methodNameUndo As String, methodNameRedo As String, paramsUndo As Object(), paramsRedo As Object(), methodFlagsUndo As BindingFlags, methodFlagsRedo As BindingFlags)
[Object] = obj
ParametersUndo.AddRange(paramsUndo)
ParametersRedo.AddRange(paramsRedo)
MethodUndo = GetMethodInfo(obj, methodNameUndo, GetFlags(methodFlagsUndo))
MethodRedo = GetMethodInfo(obj, methodNameRedo, GetFlags(methodFlagsRedo))
End Sub
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The name of the methode to call on Undo.
''' The name of the methode to call on Redo.
''' The Binding Flags of Methode on Undo.
''' The Binding Flags of Methode on Redo.
Public Sub New(obj As Object, methodNameUndo As String, methodNameRedo As String, methodFlagsUndo As BindingFlags, methodFlagsRedo As BindingFlags)
Me.New(obj, methodNameUndo, methodNameRedo, {}, {}, methodFlagsUndo, methodFlagsRedo)
End Sub
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The MethodInfo of the methode to call on Undo.
''' The MethodInfo of the methode to call on Redo.
Public Sub New(obj As Object, methodUndo As MethodInfo, methodRedo As MethodInfo)
[Object] = obj
Me.MethodUndo = methodUndo
Me.MethodRedo = methodRedo
End Sub
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The MethodInfo of the methode to call on Undo.
''' The MethodInfo of the methode to call on Redo.
''' The parameters for calling the methode on Undo.
''' The parameters for calling the methode on Redo.
Public Sub New(obj As Object, methodUndo As MethodInfo, methodRedo As MethodInfo, paramsUndo As Object(), paramsRedo As Object())
Me.New(obj, methodUndo, methodRedo)
ParametersUndo.AddRange(paramsUndo)
ParametersRedo.AddRange(paramsRedo)
End Sub
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The Delegate of the methode to call on Undo.
''' The Delegate of the methode to call on Redo.
Public Sub New(obj As Object, methodUndo As [Delegate], methodRedo As [Delegate])
[Object] = obj
Me.MethodUndo = methodUndo.Method
Me.MethodRedo = methodRedo.Method
End Sub
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The Delegate of the methode to call on Undo.
''' The Delegate of the methode to call on Redo.
''' The parameters for calling the methode on Undo.
''' The parameters for calling the methode on Redo.
Public Sub New(obj As Object, methodUndo As [Delegate], methodRedo As [Delegate], paramsUndo As Object(), paramsRedo As Object())
Me.New(obj, methodUndo, methodRedo)
ParametersUndo.AddRange(paramsUndo)
ParametersRedo.AddRange(paramsRedo)
End Sub
'''
''' Creates a new Instance of Object Action.
'''
''' The Objects that contains the methodes to call.
''' The Action of the methode to call on Undo.
''' The Action of the methode to call on Redo.
Public Sub New(obj As Object, methodUndo As Action, methodRedo As Action)
[Object] = obj
Me.MethodUndo = methodUndo.Method
Me.MethodRedo = methodRedo.Method
End Sub
Private Function GetFlags(flags As BindingFlags) As BindingFlags
If flags = BindingFlags.Default Then
flags = BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic
End If
Return flags
End Function
Friend Sub Undo()
CheckIfObjIsNothing(MethodUndo)
MethodUndo?.Invoke([Object], ParametersUndo.ToArray)
End Sub
Friend Sub Redo()
CheckIfObjIsNothing(MethodRedo)
MethodRedo?.Invoke([Object], ParametersRedo.ToArray)
End Sub
Private Sub CheckIfObjIsNothing(mi As MethodInfo)
If mi IsNot Nothing AndAlso Not mi.IsStatic AndAlso AutogenerateObject Then
If (Me.Object Is Nothing OrElse Me.Object.GetType <> mi.ReflectedType) AndAlso Not mi.IsStatic Then
Dim constructor As ConstructorInfo = mi.ReflectedType.GetConstructor(Type.EmptyTypes)
Dim classObject As Object = constructor.Invoke({})
Me.Object = classObject
End If
End If
End Sub
End Class
End Namespace