CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
UndoableHandler.cs
Go to the documentation of this file.
1#if !NETFX_CORE && !IOS
2//-----------------------------------------------------------------------
3// <copyright file="UndoableHandler.cs" company="Marimer LLC">
4// Copyright (c) Marimer LLC. All rights reserved.
5// Website: https://cslanet.com
6// </copyright>
7// <summary>no summary</summary>
8//-----------------------------------------------------------------------
9using System;
10using System.Collections.Generic;
11using System.Reflection;
12using Csla.Reflection;
13
14namespace Csla.Core
15{
16 static class UndoableHandler
17 {
18 private static readonly Dictionary<Type, List<DynamicMemberHandle>> _undoableFieldCache = new Dictionary<Type, List<DynamicMemberHandle>>();
19
20 public static List<DynamicMemberHandle> GetCachedFieldHandlers(Type type)
21 {
22 List<DynamicMemberHandle> handlers;
23 if(!_undoableFieldCache.TryGetValue(type, out handlers))
24 {
25 var newHandlers = BuildHandlers(type);
26 lock (_undoableFieldCache) //ready to add, lock
27 {
28 if(!_undoableFieldCache.TryGetValue(type, out handlers))
29 {
30 _undoableFieldCache.Add(type, newHandlers);
31 handlers = newHandlers;
32 }
33 }
34 }
35 return handlers;
36 }
37
38 private static List<DynamicMemberHandle> BuildHandlers(Type type)
39 {
40 var handlers = new List<DynamicMemberHandle>();
41 // get the list of fields in this type
42 var fields = type.GetFields(
43 BindingFlags.NonPublic |
44 BindingFlags.Instance |
45 BindingFlags.Public);
46
47 foreach (FieldInfo field in fields)
48 {
49 // make sure we process only our variables
50 if (field.DeclaringType == type)
51 {
52 // see if this field is marked as not undoable
53 if (!NotUndoableField(field))
54 {
55 // the field is undoable, so it needs to be processed.
56 handlers.Add(new DynamicMemberHandle(field));
57 }
58 }
59 }
60 return handlers;
61 }
62
63 private static bool NotUndoableField(FieldInfo field)
64 {
65 // see if this field is marked as not undoable or IsInitOnly (ie: readonly property)
66 return field.IsInitOnly || Attribute.IsDefined(field, typeof(NotUndoableAttribute));
67 }
68
69 }
70}
71#endif