39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Linq.Expressions;
|
|
using System.Reflection;
|
|
using Intromat.Interfaces;
|
|
using ReactiveUI;
|
|
|
|
namespace Intromat.ViewModels
|
|
{
|
|
public class UndoItem<TObservable, TValue> : IUndoItem where TObservable : ReactiveObject
|
|
{
|
|
private readonly TValue _newValue;
|
|
private readonly TObservable _observable;
|
|
private readonly TValue _oldValue;
|
|
private readonly PropertyInfo _propertyInfo;
|
|
|
|
public UndoItem(IFile file, TObservable observable, Expression<Func<TObservable, TValue>> memberLamda, TValue oldValue, TValue newValue)
|
|
{
|
|
File = file;
|
|
_observable = observable;
|
|
_oldValue = oldValue;
|
|
_newValue = newValue;
|
|
if (memberLamda.Body is MemberExpression { Member: PropertyInfo propertyInfo }) _propertyInfo = propertyInfo;
|
|
Debug.Assert(_propertyInfo != null);
|
|
}
|
|
|
|
public IFile File { get; }
|
|
|
|
public void Redo()
|
|
{
|
|
_propertyInfo.SetValue(_observable, _newValue);
|
|
}
|
|
|
|
public void Undo()
|
|
{
|
|
_propertyInfo.SetValue(_observable, _oldValue);
|
|
}
|
|
}
|
|
} |