10using System.Collections.Generic;
11using System.Reflection;
16 static class UndoableHandler
18 private static readonly Dictionary<Type, List<DynamicMemberHandle>> _undoableFieldCache =
new Dictionary<Type, List<DynamicMemberHandle>>();
20 public static List<DynamicMemberHandle> GetCachedFieldHandlers(Type type)
22 List<DynamicMemberHandle> handlers;
23 if(!_undoableFieldCache.TryGetValue(type, out handlers))
25 var newHandlers = BuildHandlers(type);
26 lock (_undoableFieldCache)
28 if(!_undoableFieldCache.TryGetValue(type, out handlers))
30 _undoableFieldCache.Add(type, newHandlers);
31 handlers = newHandlers;
38 private static List<DynamicMemberHandle> BuildHandlers(Type type)
40 var handlers =
new List<DynamicMemberHandle>();
42 var fields = type.GetFields(
43 BindingFlags.NonPublic |
44 BindingFlags.Instance |
47 foreach (FieldInfo field
in fields)
50 if (field.DeclaringType == type)
53 if (!NotUndoableField(field))
56 handlers.Add(
new DynamicMemberHandle(field));
63 private static bool NotUndoableField(FieldInfo field)
66 return field.IsInitOnly || Attribute.IsDefined(field, typeof(NotUndoableAttribute));