convert Pilz.Collections to c#

This commit is contained in:
Pascal Schedel
2024-10-21 09:12:54 +02:00
parent 66b270fc17
commit 34c4f1c727
20 changed files with 1288 additions and 1049 deletions

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
</configuration>

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<StartupObject />
<TargetFrameworks>net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<Version>2.0.0</Version>
</PropertyGroup>
</Project>

View File

@@ -1,62 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<StartupObject />
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<DocumentationFile>Pilz.Collections.xml</DocumentationFile>
<DefineTrace>true</DefineTrace>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DefineDebug>true</DefineDebug>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022,40008</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DefineDebug>false</DefineDebug>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
<RemoveIntegerChecks>true</RemoveIntegerChecks>
</PropertyGroup>
<PropertyGroup>
<OptionExplicit>On</OptionExplicit>
</PropertyGroup>
<PropertyGroup>
<OptionCompare>Binary</OptionCompare>
</PropertyGroup>
<PropertyGroup>
<OptionStrict>Off</OptionStrict>
<OptionInfer>On</OptionInfer>
</PropertyGroup>
<PropertyGroup>
<Version>2.0.0</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DefineDebug>true</DefineDebug>
<OutputPath>bin\$(Platform)\$(Configuration)\</OutputPath>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022,40008</NoWarn>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\$(Platform)\$(Configuration)\</OutputPath>
<RemoveIntegerChecks>true</RemoveIntegerChecks>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualBasic" Version="10.3.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
<ItemGroup>
<Import Include="Microsoft.VisualBasic" />
<Import Include="System" />
<Import Include="System.Collections" />
<Import Include="System.Collections.Generic" />
<Import Include="System.Data" />
<Import Include="System.Drawing" />
<Import Include="System.Linq" />
<Import Include="System.Xml.Linq" />
<Import Include="System.Threading.Tasks" />
</ItemGroup>
<ItemGroup>
<Compile Remove="SimpleHistory\Enums.vb" />
</ItemGroup>
</Project>

View File

@@ -1,12 +0,0 @@
Namespace SimpleHistory
''' <summary>
''' Specify which member types you would include.
''' </summary>
Public Enum ObjectValueType
None = 0
Field = 1
[Property] = 2
End Enum
End Namespace

View File

@@ -0,0 +1,615 @@
using System.Data;
using System.Reflection;
namespace Pilz.Collections.SimpleHistory;
/// <summary>
/// Represent some Object States and Actions.
/// </summary>
public class HistoryPoint
{
/// <summary>
/// Represents the Name of this History Point
/// </summary>
/// <returns></returns>
public string Name { get; set; } = string.Empty;
/// <summary>
/// A List of Object States and Actions.
/// </summary>
/// <returns></returns>
public List<ObjectBase> Entries { get; } = [];
/// <summary>
/// Some data can be refered on this HistoryPoint. Don't know, in some situations this can be helpful.
/// </summary>
public readonly object? Tag = null;
public bool HasEntries<T>() where T : ObjectBase
{
return Entries.Where(n => n is T).Any();
}
internal void Undo()
{
foreach (var s in Entries.OrderBy(n => n.UndoPriority))
{
switch (s)
{
case ObjectState state:
state.Patch();
break;
case ObjectAction action:
action.Undo();
break;
}
}
}
internal void Redo()
{
foreach (var s in Entries.OrderBy(n => n.RedoPriority))
{
switch (s)
{
case ObjectState state:
state.Patch();
break;
case ObjectAction action:
action.Redo();
break;
}
}
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="whiteList">Specify which members to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, MemberWhiteList whiteList)
{
return FromObject([obj], ObjectValueType.None, (object)whiteList, BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="blackList">Specify which members to exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, MemberBlackList blackList)
{
return FromObject([obj], ObjectValueType.None, (object)blackList, BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="memberName">The member names to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, params string[] memberName)
{
return FromObject(obj, true, memberName);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, bool isWhiteList, params string[] memberName)
{
if (isWhiteList)
return FromObject([obj], ObjectValueType.None, (object)new MemberWhiteList(memberName), BindingFlags.Default);
else
return FromObject([obj], ObjectValueType.None, (object)new MemberBlackList(memberName), BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="memberName">The member names to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, ObjectValueType membersToStore, params string[] memberName)
{
return FromObject(obj, membersToStore, true, memberName);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, ObjectValueType membersToStore, bool isWhiteList, params string[] memberName)
{
if (isWhiteList)
return FromObject([obj], membersToStore, (object)new MemberWhiteList(memberName), BindingFlags.Default);
else
return FromObject([obj], membersToStore, (object)new MemberBlackList(memberName), BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <param name="memberName">The member names to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, BindingFlags flags, params string[] memberName)
{
return FromObject(obj, flags, true, memberName);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, BindingFlags flags, bool isWhiteList, params string[] memberName)
{
if (isWhiteList)
return FromObject([obj], ObjectValueType.None, (object)new MemberWhiteList(memberName), flags);
else
return FromObject([obj], ObjectValueType.None, (object)new MemberBlackList(memberName), flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <param name="memberName">The member names to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, ObjectValueType membersToStore, BindingFlags flags, params string[] memberName)
{
return FromObject(obj, flags, true, memberName);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] obj, ObjectValueType membersToStore, BindingFlags flags, bool isWhiteList, params string[] memberName)
{
if (isWhiteList)
return FromObject([obj], membersToStore, (object)new MemberWhiteList(memberName), flags);
else
return FromObject([obj], membersToStore, (object)new MemberBlackList(memberName), flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="whiteList">Specify which members to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, MemberWhiteList whiteList)
{
return FromObject([obj], ObjectValueType.None, (object)whiteList, BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="blackList">Specify which members to exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, MemberBlackList blackList)
{
return FromObject([obj], ObjectValueType.None, (object)blackList, BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, params string[] memberName)
{
return FromObject(obj, true, memberName);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, bool isWhiteList, params string[] memberName)
{
if (isWhiteList)
return FromObject([obj], ObjectValueType.None, (object)new MemberWhiteList(memberName), BindingFlags.Default);
else
return FromObject([obj], ObjectValueType.None, (object)new MemberBlackList(memberName), BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="memberName">The member names to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, params string[] memberName)
{
return FromObject(obj, membersToStore, true, memberName);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, bool isWhiteList, params string[] memberName)
{
if (isWhiteList)
return FromObject([obj], membersToStore, (object)new MemberWhiteList(memberName), BindingFlags.Default);
else
return FromObject([obj], membersToStore, (object)new MemberBlackList(memberName), BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <param name="memberName">The member names to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, BindingFlags flags, params string[] memberName)
{
return FromObject(obj, flags, true, memberName);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, BindingFlags flags, bool isWhiteList, params string[] memberName)
{
if (isWhiteList)
return FromObject([obj], ObjectValueType.None, (object)new MemberWhiteList(memberName), flags);
else
return FromObject([obj], ObjectValueType.None, (object)new MemberBlackList(memberName), flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <param name="memberName">The member names to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, BindingFlags flags, params string[] memberName)
{
return FromObject(obj, flags, true, memberName);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
/// <param name="memberName">The member names to include/exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, BindingFlags flags, bool isWhiteList, params string[] memberName)
{
if (isWhiteList)
return FromObject([obj], membersToStore, (object)new MemberWhiteList(memberName), flags);
else
return FromObject([obj], membersToStore, (object)new MemberBlackList(memberName), flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj)
{
return FromObject([obj], ObjectValueType.None, default(object), BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore)
{
return FromObject([obj], membersToStore, default(object), BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="whiteList">Specify which members to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, MemberWhiteList whiteList)
{
return FromObject([obj], membersToStore, (object)whiteList, BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="blackList">Specify which members to exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, MemberBlackList blackList)
{
return FromObject([obj], membersToStore, (object)blackList, BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, BindingFlags flags)
{
return FromObject([obj], ObjectValueType.None, default(object), flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, BindingFlags flags)
{
return FromObject([obj], membersToStore, default(object), flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="whiteList">Specify which members to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, MemberWhiteList whiteList, BindingFlags flags)
{
return FromObject([obj], membersToStore, (object)whiteList, flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="obj">The object that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="blackList">Specify which members to exclude.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object obj, ObjectValueType membersToStore, MemberBlackList blackList, BindingFlags flags)
{
return FromObject([obj], membersToStore, (object)blackList, flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] objs)
{
return FromObject(objs, ObjectValueType.None, default(object), BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] objs, ObjectValueType membersToStore)
{
return FromObject(objs, membersToStore, default(object), BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="whiteList">Specify which members to include.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] objs, ObjectValueType membersToStore, MemberWhiteList whiteList)
{
return FromObject(objs, membersToStore, (object)whiteList, BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="blackList">Specify which members to exclude.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] objs, ObjectValueType membersToStore, MemberBlackList blackList)
{
return FromObject(objs, membersToStore, (object)blackList, BindingFlags.Default);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] objs, BindingFlags flags)
{
return FromObject(objs, ObjectValueType.None, default(object), flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] objs, ObjectValueType membersToStore, BindingFlags flags)
{
return FromObject(objs, membersToStore, default(object), flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="whiteList">Specify which members to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] objs, ObjectValueType membersToStore, MemberWhiteList whiteList, BindingFlags flags)
{
return FromObject(objs, membersToStore, (object)whiteList, flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="blackList">Specify which members to exclude.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
public static HistoryPoint FromObject(object[] objs, ObjectValueType membersToStore, MemberBlackList blackList, BindingFlags flags)
{
return FromObject(objs, membersToStore, (object)blackList, flags);
}
/// <summary>
/// Creates an History Point with Object States automaticly from input.
/// </summary>
/// <param name="objs">The objects that should be included.</param>
/// <param name="membersToStore">Specify what member types to include.</param>
/// <param name="whiteOrBlackList">Specify which members to include.</param>
/// <param name="flags">The Binding Flags that the members should have.</param>
/// <returns>A History Point with Object States.</returns>
private static HistoryPoint FromObject(object[] objs, ObjectValueType membersToStore, object? whiteOrBlackList, BindingFlags flags)
{
var hp = new HistoryPoint();
whiteOrBlackList ??= new MemberBlackList();
var isWhiteList = whiteOrBlackList is MemberWhiteList;
if (flags == BindingFlags.Default)
flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
if (membersToStore == ObjectValueType.None)
membersToStore = ObjectValueType.Field | ObjectValueType.Property;
foreach (var obj in objs)
{
if ((membersToStore & ObjectValueType.Field) == ObjectValueType.Field)
{
foreach (var fi in obj.GetType().GetFields(flags))
{
var contains = ((List<string>)whiteOrBlackList).Contains(fi.Name);
if (isWhiteList ? contains : !contains)
{
var os = new ObjectState
{
Object = obj,
MemberName = fi.Name,
MemberType = ObjectValueType.Field,
MemberFlags = flags,
ValueToPatch = fi.GetValue(obj)
};
hp.Entries.Add(os);
}
}
}
if ((membersToStore & ObjectValueType.Property) == ObjectValueType.Property)
{
foreach (var pi in obj.GetType().GetProperties(flags))
{
var contains = ((List<string>)whiteOrBlackList).Contains(pi.Name);
if (isWhiteList ? contains : !contains)
{
var os = new ObjectState
{
Object = obj,
MemberName = pi.Name,
MemberType = ObjectValueType.Property,
MemberFlags = flags,
ValueToPatch = pi.GetValue(obj)
};
hp.Entries.Add(os);
}
}
}
}
return hp;
}
/// <summary>
/// Combines some History Points to one.
/// </summary>
/// <param name="hps">An array of History Points to combine.</param>
/// <returns>One History Point that contains all Data of inputted History Points.</returns>
public static HistoryPoint Concat(params HistoryPoint[] hps)
{
return Concat(null, hps);
}
/// <summary>
/// Combines some History Points to one.
/// </summary>
/// <param name="hps">An array of History Points to combine.</param>
/// <param name="newName">The new name for the History Point after concating.</param>
/// <returns>One History Point that contains all Data of inputted History Points.</returns>
public static HistoryPoint Concat(string? newName, params HistoryPoint[] hps)
{
var hp = new HistoryPoint();
foreach (var _hp in hps)
hp.Entries.AddRange(_hp.Entries);
if (newName != null)
hp.Name = newName;
else
hp.Name = hps.FirstOrDefault()?.Name ?? string.Empty;
return hp;
}
}

View File

@@ -1,543 +0,0 @@
Imports System.Reflection
Namespace SimpleHistory
''' <summary>
''' Represent some Object States and Actions.
''' </summary>
Public Class HistoryPoint
''' <summary>
''' Represents the Name of this History Point
''' </summary>
''' <returns></returns>
Public Property Name As String = ""
''' <summary>
''' A List of Object States and Actions.
''' </summary>
''' <returns></returns>
Public ReadOnly Property Entries As New List(Of ObjectBase)
''' <summary>
''' Some data can be refered on this HistoryPoint. Don't know, in some situations this can be helpful.
''' </summary>
Public ReadOnly Tag As Object = Nothing
Public Function HasEntries(Of T As ObjectBase)() As Boolean
Return Entries.Where(Function(n) TypeOf n Is T).Count > 0
End Function
Friend Sub Undo()
For Each s As ObjectBase In Entries.OrderBy(Function(n) n.UndoPriority)
If TypeOf s Is ObjectState Then
CType(s, ObjectState).Patch()
ElseIf TypeOf s Is ObjectAction Then
CType(s, ObjectAction).Undo()
End If
Next
End Sub
Friend Sub Redo()
For Each s As ObjectBase In Entries.OrderBy(Function(n) n.RedoPriority)
If TypeOf s Is ObjectState Then
CType(s, ObjectState).Patch()
ElseIf TypeOf s Is ObjectAction Then
CType(s, ObjectAction).Redo()
End If
Next
End Sub
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="whiteList">Specify which members to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), whiteList As MemberWhiteList) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(whiteList), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="blackList">Specify which members to exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), blackList As MemberBlackList) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(blackList), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="memberName">The member names to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, True, memberName)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberWhiteList(memberName)), BindingFlags.Default)
Else
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberBlackList(memberName)), BindingFlags.Default)
End If
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="memberName">The member names to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), membersToStore As ObjectValueType, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, membersToStore, True, memberName)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), membersToStore As ObjectValueType, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, membersToStore, CObj(New MemberWhiteList(memberName)), BindingFlags.Default)
Else
Return FromObject({obj}, membersToStore, CObj(New MemberBlackList(memberName)), BindingFlags.Default)
End If
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <param name="memberName">The member names to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), flags As BindingFlags, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, flags, True, memberName)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), flags As BindingFlags, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberWhiteList(memberName)), flags)
Else
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberBlackList(memberName)), flags)
End If
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <param name="memberName">The member names to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), membersToStore As ObjectValueType, flags As BindingFlags, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, flags, True, memberName)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object(), membersToStore As ObjectValueType, flags As BindingFlags, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, membersToStore, CObj(New MemberWhiteList(memberName)), flags)
Else
Return FromObject({obj}, membersToStore, CObj(New MemberBlackList(memberName)), flags)
End If
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="whiteList">Specify which members to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, whiteList As MemberWhiteList) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(whiteList), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="blackList">Specify which members to exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, blackList As MemberBlackList) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(blackList), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, True, memberName)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberWhiteList(memberName)), BindingFlags.Default)
Else
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberBlackList(memberName)), BindingFlags.Default)
End If
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="memberName">The member names to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, membersToStore, True, memberName)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, membersToStore, CObj(New MemberWhiteList(memberName)), BindingFlags.Default)
Else
Return FromObject({obj}, membersToStore, CObj(New MemberBlackList(memberName)), BindingFlags.Default)
End If
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <param name="memberName">The member names to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, flags As BindingFlags, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, flags, True, memberName)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, flags As BindingFlags, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberWhiteList(memberName)), flags)
Else
Return FromObject({obj}, ObjectValueType.None, CObj(New MemberBlackList(memberName)), flags)
End If
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <param name="memberName">The member names to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, flags As BindingFlags, ParamArray memberName As String()) As HistoryPoint
Return FromObject(obj, flags, True, memberName)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <param name="isWhiteList">If true, the memberName-Array has member names that should be included.</param>
''' <param name="memberName">The member names to include/exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, flags As BindingFlags, isWhiteList As Boolean, ParamArray memberName As String()) As HistoryPoint
If isWhiteList Then
Return FromObject({obj}, membersToStore, CObj(New MemberWhiteList(memberName)), flags)
Else
Return FromObject({obj}, membersToStore, CObj(New MemberBlackList(memberName)), flags)
End If
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(Nothing), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(Nothing), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="whiteList">Specify which members to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, whiteList As MemberWhiteList) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(whiteList), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="blackList">Specify which members to exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, blackList As MemberBlackList) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(blackList), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, flags As BindingFlags) As HistoryPoint
Return FromObject({obj}, ObjectValueType.None, CObj(Nothing), flags)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, flags As BindingFlags) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(Nothing), flags)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="whiteList">Specify which members to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, whiteList As MemberWhiteList, flags As BindingFlags) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(whiteList), flags)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="obj">The object that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="blackList">Specify which members to exclude.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(obj As Object, membersToStore As ObjectValueType, blackList As MemberBlackList, flags As BindingFlags) As HistoryPoint
Return FromObject({obj}, membersToStore, CObj(blackList), flags)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(objs As Object()) As HistoryPoint
Return FromObject(objs, ObjectValueType.None, CObj(Nothing), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(Nothing), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="whiteList">Specify which members to include.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, whiteList As MemberWhiteList) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(whiteList), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="blackList">Specify which members to exclude.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, blackList As MemberBlackList) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(blackList), BindingFlags.Default)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(objs As Object(), flags As BindingFlags) As HistoryPoint
Return FromObject(objs, ObjectValueType.None, CObj(Nothing), flags)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, flags As BindingFlags) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(Nothing), flags)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="whiteList">Specify which members to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, whiteList As MemberWhiteList, flags As BindingFlags) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(whiteList), flags)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="blackList">Specify which members to exclude.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Public Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, blackList As MemberBlackList, flags As BindingFlags) As HistoryPoint
Return FromObject(objs, membersToStore, CObj(blackList), flags)
End Function
''' <summary>
''' Creates an History Point with Object States automaticly from input.
''' </summary>
''' <param name="objs">The objects that should be included.</param>
''' <param name="membersToStore">Specify what member types to include.</param>
''' <param name="whiteOrBlackList">Specify which members to include.</param>
''' <param name="flags">The Binding Flags that the members should have.</param>
''' <returns>A History Point with Object States.</returns>
Private Shared Function FromObject(objs As Object(), membersToStore As ObjectValueType, whiteOrBlackList As Object, flags As BindingFlags) As HistoryPoint
Dim hp As New HistoryPoint
If whiteOrBlackList Is Nothing Then whiteOrBlackList = New MemberBlackList
Dim isWhiteList As Boolean = TypeOf whiteOrBlackList Is MemberWhiteList
If flags = BindingFlags.Default Then
flags = BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic
End If
If membersToStore = ObjectValueType.None Then
membersToStore = ObjectValueType.Field Or ObjectValueType.Property
End If
For Each obj As Object In objs
If (membersToStore And ObjectValueType.Field) = ObjectValueType.Field Then
For Each fi As FieldInfo In obj.GetType.GetFields(flags)
Dim contains As Boolean = CType(whiteOrBlackList, List(Of String)).Contains(fi.Name)
If If(isWhiteList, contains, Not contains) Then
Dim os As New ObjectState
os.Object = obj
os.MemberName = fi.Name
os.MemberType = ObjectValueType.Field
os.MemberFlags = flags
os.ValueToPatch = fi.GetValue(obj)
hp.Entries.Add(os)
End If
Next
End If
If (membersToStore And ObjectValueType.Property) = ObjectValueType.Property Then
For Each pi As PropertyInfo In obj.GetType.GetProperties(flags)
Dim contains As Boolean = CType(whiteOrBlackList, List(Of String)).Contains(pi.Name)
If If(isWhiteList, contains, Not contains) Then
Dim os As New ObjectState
os.Object = obj
os.MemberName = pi.Name
os.MemberType = ObjectValueType.Property
os.MemberFlags = flags
os.ValueToPatch = pi.GetValue(obj)
hp.Entries.Add(os)
End If
Next
End If
Next
Return hp
End Function
''' <summary>
''' Combines some History Points to one.
''' </summary>
''' <param name="hps">An array of History Points to combine.</param>
''' <returns>One History Point that contains all Data of inputted History Points.</returns>
Public Shared Function Concat(ParamArray hps As HistoryPoint()) As HistoryPoint
Return Concat(hps.FirstOrDefault?.Name, hps)
End Function
''' <summary>
''' Combines some History Points to one.
''' </summary>
''' <param name="hps">An array of History Points to combine.</param>
''' <returns>One History Point that contains all Data of inputted History Points.</returns>
Public Shared Function Concat(newName As String, ParamArray hps As HistoryPoint()) As HistoryPoint
Dim hp As New HistoryPoint
For Each _hp As HistoryPoint In hps
hp.Entries.AddRange(_hp.Entries)
Next
Return hp
End Function
End Class
End Namespace

View File

@@ -0,0 +1,31 @@
namespace Pilz.Collections.SimpleHistory;
/// <summary>
/// List contianing member names to include.
/// </summary>
public class MemberWhiteList : List<string>
{
public MemberWhiteList() : base()
{
}
public MemberWhiteList(string[] entries) : base(entries)
{
}
}
/// <summary>
/// List contianing member names to exclude
/// </summary>
public class MemberBlackList : List<string>
{
public MemberBlackList() : base()
{
}
public MemberBlackList(string[] entries) : base(entries)
{
}
}

View File

@@ -1,33 +0,0 @@
Namespace SimpleHistory
''' <summary>
''' List contianing member names to include.
''' </summary>
Public Class MemberWhiteList
Inherits List(Of String)
Public Sub New()
MyBase.New
End Sub
Public Sub New(entries As String())
MyBase.New(entries)
End Sub
End Class
''' <summary>
''' List contianing member names to exclude
''' </summary>
Public Class MemberBlackList
Inherits List(Of String)
Public Sub New()
MyBase.New
End Sub
Public Sub New(entries As String())
MyBase.New(entries)
End Sub
End Class
End Namespace

View File

@@ -0,0 +1,176 @@
using System.Reflection;
namespace Pilz.Collections.SimpleHistory;
public class ObjectAction : ObjectBase
{
public object? Object { get; set; } = null;
public List<object> ParametersUndo { get; } = [];
public List<object> ParametersRedo { get; } = [];
public MethodInfo? MethodUndo { get; set; } = null;
public MethodInfo? MethodRedo { get; set; } = null;
public bool AutogenerateObject { get; set; } = true;
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
public ObjectAction()
{
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodNameUndo">The name of the methode to call on Undo.</param>
/// <param name="methodNameRedo">The name of the methode to call on Redo.</param>
public ObjectAction(object obj, string methodNameUndo, string methodNameRedo) : this(obj, methodNameUndo, methodNameRedo, [], [], BindingFlags.Default, BindingFlags.Default)
{
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodNameUndo">The name of the methode to call on Undo.</param>
/// <param name="methodNameRedo">The name of the methode to call on Redo.</param>
/// <param name="paramsUndo">The parameters for calling the methode on Undo.</param>
/// <param name="paramsRedo">The parameters for calling the methode on Redo.</param>
public ObjectAction(object obj, string methodNameUndo, string methodNameRedo, object[] paramsUndo, object[] paramsRedo) : this(obj, methodNameUndo, methodNameRedo, paramsUndo, paramsRedo, BindingFlags.Default, BindingFlags.Default)
{
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodNameUndo">The name of the methode to call on Undo.</param>
/// <param name="methodNameRedo">The name of the methode to call on Redo.</param>
/// <param name="paramsUndo">The parameters for calling the methode on Undo.</param>
/// <param name="paramsRedo">The parameters for calling the methode on Redo.</param>
/// <param name="methodFlagsUndo">The Binding Flags of Methode on Undo.</param>
/// <param name="methodFlagsRedo">The Binding Flags of Methode on Redo.</param>
public ObjectAction(object obj, string methodNameUndo, string methodNameRedo, object[] paramsUndo, object[] paramsRedo, BindingFlags methodFlagsUndo, BindingFlags methodFlagsRedo)
{
Object = obj;
ParametersUndo.AddRange(paramsUndo);
ParametersRedo.AddRange(paramsRedo);
MethodUndo = GetMethodInfo(obj, methodNameUndo, GetFlags(methodFlagsUndo));
MethodRedo = GetMethodInfo(obj, methodNameRedo, GetFlags(methodFlagsRedo));
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodNameUndo">The name of the methode to call on Undo.</param>
/// <param name="methodNameRedo">The name of the methode to call on Redo.</param>
/// <param name="methodFlagsUndo">The Binding Flags of Methode on Undo.</param>
/// <param name="methodFlagsRedo">The Binding Flags of Methode on Redo.</param>
public ObjectAction(object obj, string methodNameUndo, string methodNameRedo, BindingFlags methodFlagsUndo, BindingFlags methodFlagsRedo) : this(obj, methodNameUndo, methodNameRedo, [], [], methodFlagsUndo, methodFlagsRedo)
{
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodUndo">The MethodInfo of the methode to call on Undo.</param>
/// <param name="methodRedo">The MethodInfo of the methode to call on Redo.</param>
public ObjectAction(object obj, MethodInfo methodUndo, MethodInfo methodRedo)
{
Object = obj;
MethodUndo = methodUndo;
MethodRedo = methodRedo;
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodUndo">The MethodInfo of the methode to call on Undo.</param>
/// <param name="methodRedo">The MethodInfo of the methode to call on Redo.</param>
/// <param name="paramsUndo">The parameters for calling the methode on Undo.</param>
/// <param name="paramsRedo">The parameters for calling the methode on Redo.</param>
public ObjectAction(object obj, MethodInfo methodUndo, MethodInfo methodRedo, object[] paramsUndo, object[] paramsRedo) : this(obj, methodUndo, methodRedo)
{
ParametersUndo.AddRange(paramsUndo);
ParametersRedo.AddRange(paramsRedo);
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodUndo">The Delegate of the methode to call on Undo.</param>
/// <param name="methodRedo">The Delegate of the methode to call on Redo.</param>
public ObjectAction(object obj, Delegate methodUndo, Delegate methodRedo)
{
Object = obj;
MethodUndo = methodUndo.Method;
MethodRedo = methodRedo.Method;
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodUndo">The Delegate of the methode to call on Undo.</param>
/// <param name="methodRedo">The Delegate of the methode to call on Redo.</param>
/// <param name="paramsUndo">The parameters for calling the methode on Undo.</param>
/// <param name="paramsRedo">The parameters for calling the methode on Redo.</param>
public ObjectAction(object obj, Delegate methodUndo, Delegate methodRedo, object[] paramsUndo, object[] paramsRedo) : this(obj, methodUndo, methodRedo)
{
ParametersUndo.AddRange(paramsUndo);
ParametersRedo.AddRange(paramsRedo);
}
/// <summary>
/// Creates a new Instance of Object Action.
/// </summary>
/// <param name="obj">The Objects that contains the methodes to call.</param>
/// <param name="methodUndo">The Action of the methode to call on Undo.</param>
/// <param name="methodRedo">The Action of the methode to call on Redo.</param>
public ObjectAction(object obj, Action methodUndo, Action methodRedo)
{
Object = obj;
MethodUndo = methodUndo.Method;
MethodRedo = methodRedo.Method;
}
private static BindingFlags GetFlags(BindingFlags flags)
{
if (flags == BindingFlags.Default)
flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
return flags;
}
private static MethodInfo? GetMethodInfo(object obj, string name, BindingFlags flags)
{
return obj.GetType().GetMethod(name, flags);
}
internal void Undo()
{
CheckIfObjIsNothing(MethodUndo);
MethodUndo?.Invoke(Object, ParametersUndo.ToArray());
}
internal void Redo()
{
CheckIfObjIsNothing(MethodRedo);
MethodRedo?.Invoke(Object, ParametersRedo.ToArray());
}
private void CheckIfObjIsNothing(MethodInfo? mi)
{
if (mi is not null && !mi.IsStatic && AutogenerateObject)
{
if ((Object is null || Object.GetType() != mi.ReflectedType) && !mi.IsStatic)
{
var constructor = mi.ReflectedType?.GetConstructor(Type.EmptyTypes);
var classObject = constructor?.Invoke([]);
Object = classObject;
}
}
}
}

View File

@@ -1,164 +0,0 @@
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
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
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
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodNameUndo">The name of the methode to call on Undo.</param>
''' <param name="methodNameRedo">The name of the methode to call on Redo.</param>
Public Sub New(obj As Object, methodNameUndo As String, methodNameRedo As String)
Me.New(obj, methodNameUndo, methodNameRedo, {}, {}, BindingFlags.Default, BindingFlags.Default)
End Sub
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodNameUndo">The name of the methode to call on Undo.</param>
''' <param name="methodNameRedo">The name of the methode to call on Redo.</param>
''' <param name="paramsUndo">The parameters for calling the methode on Undo.</param>
''' <param name="paramsRedo">The parameters for calling the methode on Redo.</param>
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
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodNameUndo">The name of the methode to call on Undo.</param>
''' <param name="methodNameRedo">The name of the methode to call on Redo.</param>
''' <param name="paramsUndo">The parameters for calling the methode on Undo.</param>
''' <param name="paramsRedo">The parameters for calling the methode on Redo.</param>
''' <param name="methodFlagsUndo">The Binding Flags of Methode on Undo.</param>
''' <param name="methodFlagsRedo">The Binding Flags of Methode on Redo.</param>
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
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodNameUndo">The name of the methode to call on Undo.</param>
''' <param name="methodNameRedo">The name of the methode to call on Redo.</param>
''' <param name="methodFlagsUndo">The Binding Flags of Methode on Undo.</param>
''' <param name="methodFlagsRedo">The Binding Flags of Methode on Redo.</param>
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
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodUndo">The MethodInfo of the methode to call on Undo.</param>
''' <param name="methodRedo">The MethodInfo of the methode to call on Redo.</param>
Public Sub New(obj As Object, methodUndo As MethodInfo, methodRedo As MethodInfo)
[Object] = obj
Me.MethodUndo = methodUndo
Me.MethodRedo = methodRedo
End Sub
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodUndo">The MethodInfo of the methode to call on Undo.</param>
''' <param name="methodRedo">The MethodInfo of the methode to call on Redo.</param>
''' <param name="paramsUndo">The parameters for calling the methode on Undo.</param>
''' <param name="paramsRedo">The parameters for calling the methode on Redo.</param>
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
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodUndo">The Delegate of the methode to call on Undo.</param>
''' <param name="methodRedo">The Delegate of the methode to call on Redo.</param>
Public Sub New(obj As Object, methodUndo As [Delegate], methodRedo As [Delegate])
[Object] = obj
Me.MethodUndo = methodUndo.Method
Me.MethodRedo = methodRedo.Method
End Sub
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodUndo">The Delegate of the methode to call on Undo.</param>
''' <param name="methodRedo">The Delegate of the methode to call on Redo.</param>
''' <param name="paramsUndo">The parameters for calling the methode on Undo.</param>
''' <param name="paramsRedo">The parameters for calling the methode on Redo.</param>
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
''' <summary>
''' Creates a new Instance of Object Action.
''' </summary>
''' <param name="obj">The Objects that contains the methodes to call.</param>
''' <param name="methodUndo">The Action of the methode to call on Undo.</param>
''' <param name="methodRedo">The Action of the methode to call on Redo.</param>
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

View File

@@ -0,0 +1,10 @@
namespace Pilz.Collections.SimpleHistory;
public class ObjectBase
{
public static int DefaultPriorityValue { get; set; } = 1000;
public int UndoPriority { get; set; } = DefaultPriorityValue;
public int RedoPriority { get; set; } = DefaultPriorityValue;
}

View File

@@ -1,13 +0,0 @@
Namespace SimpleHistory
Public Class ObjectBase
Public Shared Property DefaultPriorityValue As Integer = 1000
Public Property UndoPriority As Integer = DefaultPriorityValue
Public Property RedoPriority As Integer = DefaultPriorityValue
End Class
End Namespace

View File

@@ -0,0 +1,88 @@
using System.Reflection;
namespace Pilz.Collections.SimpleHistory;
public class ObjectState : ObjectBase
{
/// <summary>
/// The Object including the members to patch.
/// </summary>
/// <returns></returns>
public object? Object { get; set; } = null;
/// <summary>
/// The name of the Member to patch.
/// </summary>
/// <returns></returns>
public string MemberName { get; set; } = "";
/// <summary>
/// The Value that should be patched.
/// </summary>
/// <returns></returns>
public object? ValueToPatch { get; set; } = null;
/// <summary>
/// The member types to include at searching for the member.
/// </summary>
/// <returns></returns>
public ObjectValueType MemberType { get; set; } = ObjectValueType.Field;
/// <summary>
/// The Binding Flags that are used at searching for the member.
/// </summary>
/// <returns></returns>
public BindingFlags MemberFlags { get; set; } = BindingFlags.Default;
/// <summary>
/// Creates a new Instance of ObjectState from input.
/// </summary>
/// <param name="obj">The Object including the members to patch.</param>
/// <param name="valname">The name of the Member to patch.</param>
/// <param name="valToPatch">The member types to include at searching for the member.</param>
/// <param name="valtype">The Binding Flags that are used at searching for the member.</param>
public ObjectState(object obj, string valname, object valToPatch, ObjectValueType valtype)
{
Object = obj;
MemberName = valname;
ValueToPatch = valToPatch;
MemberType = valtype;
}
/// <summary>
/// Creates a new Instance of ObjectState.
/// </summary>
public ObjectState()
{
}
internal void Patch()
{
if (Object is null)
return;
var t = Object.GetType();
switch (MemberType)
{
case ObjectValueType.Field:
if (t.GetField(MemberName, MemberFlags) is FieldInfo f)
{
var temp = f.GetValue(Object);
f.SetValue(Object, ValueToPatch);
ValueToPatch = temp;
}
break;
case ObjectValueType.Property:
if (t.GetProperty(MemberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) is PropertyInfo p)
{
var temp = p.GetValue(Object);
p.SetValue(Object, ValueToPatch);
ValueToPatch = temp;
}
break;
default:
throw new Exception("ValueType is invalid!");
}
}
}

View File

@@ -1,84 +0,0 @@
Imports System.Reflection
Namespace SimpleHistory
Public Class ObjectState
Inherits ObjectBase
''' <summary>
''' The Object including the members to patch.
''' </summary>
''' <returns></returns>
Public Property [Object] As Object = Nothing
''' <summary>
''' The name of the Member to patch.
''' </summary>
''' <returns></returns>
Public Property MemberName As String = ""
''' <summary>
''' The Value that should be patched.
''' </summary>
''' <returns></returns>
Public Property ValueToPatch As Object = Nothing
''' <summary>
''' The member types to include at searching for the member.
''' </summary>
''' <returns></returns>
Public Property MemberType As ObjectValueType = ObjectValueType.Field
''' <summary>
''' The Binding Flags that are used at searching for the member.
''' </summary>
''' <returns></returns>
Public Property MemberFlags As BindingFlags = BindingFlags.Default
''' <summary>
''' Creates a new Instance of ObjectState from input.
''' </summary>
''' <param name="obj">The Object including the members to patch.</param>
''' <param name="valname">The name of the Member to patch.</param>
''' <param name="valToPatch">The member types to include at searching for the member.</param>
''' <param name="valtype">The Binding Flags that are used at searching for the member.</param>
Public Sub New(obj As Object, valname As String, valToPatch As Object, valtype As ObjectValueType)
[Object] = obj
MemberName = valname
ValueToPatch = valToPatch
MemberType = valtype
End Sub
''' <summary>
''' Creates a new Instance of ObjectState.
''' </summary>
Public Sub New()
End Sub
Friend Sub Patch()
Dim t As Type = [Object].GetType
Select Case MemberType
Case ObjectValueType.Field
Dim f As FieldInfo = t.GetField(MemberName, MemberFlags)
Dim temp As Object = Nothing
If f IsNot Nothing Then
temp = f.GetValue([Object])
f.SetValue([Object], ValueToPatch)
ValueToPatch = temp
End If
Case ObjectValueType.Property
Dim p As PropertyInfo = t.GetProperty(MemberName, BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Static)
Dim temp As Object = Nothing
If p IsNot Nothing Then
temp = p.GetValue([Object])
p.SetValue([Object], ValueToPatch)
ValueToPatch = temp
End If
Case Else
Throw New Exception("ValueType is invalid!")
End Select
End Sub
End Class
End Namespace

View File

@@ -0,0 +1,11 @@
namespace Pilz.Collections.SimpleHistory;
/// <summary>
/// Specify which member types you would include.
/// </summary>
public enum ObjectValueType
{
None = 0,
Field = 1,
Property = 2,
}

View File

@@ -1,12 +0,0 @@
Namespace SimpleHistory
''' <summary>
''' Specify which member types you would include.
''' </summary>
Public Enum ObjectValueType
None = 0
Field = 1
[Property] = 2
End Enum
End Namespace

View File

@@ -0,0 +1,101 @@
namespace Pilz.Collections.SimpleHistory;
public class HistoryStack
{
private readonly Stack<HistoryPoint> stackPast = new();
private readonly Stack<HistoryPoint> stackFuture = new();
/// <summary>
/// Gets the count of history points.
/// </summary>
/// <returns></returns>
public int ChangesCount => stackPast.Count;
/// <summary>
/// Gets the current stack of all past HistoryPoints that are used for the Undo function.
/// </summary>
/// <returns></returns>
public HistoryPoint[] PastHistoryPoints => [.. stackPast];
/// <summary>
/// Gets the current stack of all future HistoryPoints that are used for the Redo function.
/// </summary>
/// <returns></returns>
public HistoryPoint[] FutureHistoryPoints => [.. stackFuture];
/// <summary>
/// Checks if the History has past changes.
/// </summary>
/// <returns></returns>
public bool HasChanges() => stackPast.Count > 0;
/// <summary>
/// Patch Object States and call Undo Actions.
/// </summary>
public HistoryPoint? Undo()
{
HistoryPoint? ret;
if (stackPast.Count > 0)
{
var hp = stackPast.Pop();
hp.Undo();
stackFuture.Push(hp);
ret = hp;
}
else
ret = null;
return ret;
}
/// <summary>
/// Patch Object States and call Redo Actions.
/// </summary>
public HistoryPoint? Redo()
{
HistoryPoint? ret;
if (stackFuture.Count > 0)
{
var hp = stackFuture.Pop();
hp.Redo();
stackPast.Push(hp);
ret = hp;
}
else
ret = null;
return ret;
}
/// <summary>
/// Clear the History.
/// </summary>
public void Clear()
{
stackPast.Clear();
stackFuture.Clear();
}
/// <summary>
/// Store a History Point.
/// </summary>
/// <param name="point">The History Point to add to the past changes.</param>
/// <param name="newName">The name to set for the History Point.</param>
public void Store(HistoryPoint point, string newName)
{
point.Name = newName;
Store(point);
}
/// <summary>
/// Store a History Point.
/// </summary>
/// <param name="point">The History Point to add to the past changes.</param>
public void Store(HistoryPoint point)
{
stackPast.Push(point);
stackFuture.Clear();
}
}

View File

@@ -1,111 +0,0 @@
Namespace SimpleHistory
Public Class HistoryStack
Private stackPast As New Stack(Of HistoryPoint)
Private stackFuture As New Stack(Of HistoryPoint)
''' <summary>
''' Gets the count of history points.
''' </summary>
''' <returns></returns>
Public ReadOnly Property ChangesCount As Boolean
Get
Return stackPast.Count
End Get
End Property
''' <summary>
''' Gets the current stack of all past HistoryPoints that are used for the Undo function.
''' </summary>
''' <returns></returns>
Public ReadOnly Property PastHistoryPoints As HistoryPoint()
Get
Return stackPast.ToArray
End Get
End Property
''' <summary>
''' Gets the current stack of all future HistoryPoints that are used for the Redo function.
''' </summary>
''' <returns></returns>
Public ReadOnly Property FutureHistoryPoints As HistoryPoint()
Get
Return stackFuture.ToArray
End Get
End Property
''' <summary>
''' Checks if the History has past changes.
''' </summary>
''' <returns></returns>
Public Function HasChanges() As Boolean
Return stackPast.Count > 0
End Function
''' <summary>
''' Patch Object States and call Undo Actions.
''' </summary>
Public Function Undo() As HistoryPoint
Dim ret As HistoryPoint
If stackPast.Count > 0 Then
Dim hp As HistoryPoint = stackPast.Pop
hp.Undo()
stackFuture.Push(hp)
ret = hp
Else
ret = Nothing
End If
Return ret
End Function
''' <summary>
''' Patch Object States and call Redo Actions.
''' </summary>
Public Function Redo() As HistoryPoint
Dim ret As HistoryPoint
If stackFuture.Count > 0 Then
Dim hp As HistoryPoint = stackFuture.Pop
hp.Redo()
stackPast.Push(hp)
ret = hp
Else
ret = Nothing
End If
Return ret
End Function
''' <summary>
''' Clear the History.
''' </summary>
Public Sub Clear()
stackPast.Clear()
stackFuture.Clear()
End Sub
''' <summary>
''' Store a History Point.
''' </summary>
''' <param name="point">The History Point to add to the past changes.</param>
''' <param name="newName">The name to set for the History Point.</param>
Public Sub Store(point As HistoryPoint, newName As String)
point.Name = newName
Store(point)
End Sub
''' <summary>
''' Store a History Point.
''' </summary>
''' <param name="point">The History Point to add to the past changes.</param>
Public Sub Store(point As HistoryPoint)
stackPast.Push(point)
stackFuture.Clear()
End Sub
End Class
End Namespace