Changeset 1553 for trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs
- Timestamp:
- 04/14/09 13:23:08 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs
r1542 r1553 6 6 using System.Collections.Generic; 7 7 8 namespace HeuristicLab.Persistence.Default.Decomposers { 9 10 public class EnumerableCache { 11 readonly List<object> values; 12 int nSet; 13 int count; 14 readonly object enumerable; 15 readonly MethodInfo addMethod; 16 17 public EnumerableCache(object enumerable, MethodInfo addMethod) { 18 values = new List<object>(); 19 this.enumerable = enumerable; 20 this.addMethod = addMethod; 21 count = -1; 22 } 23 24 public Setter GetNextSetter() { 25 int index = values.Count; 26 values.Add(new object()); 27 return v => Set(index, v); 28 } 29 30 private void Set(int index, object value) { 31 values[index] = value; 32 nSet += 1; 33 if (count >= 0 && nSet >= count) 34 Fill(); 35 } 36 37 public void Terminate() { 38 count = values.Count; 39 if (nSet >= count) 40 Fill(); 41 } 42 43 private void Fill() { 44 foreach ( object v in values ) { 45 addMethod.Invoke(enumerable, new[] {v}); 46 } 47 } 48 49 } 8 namespace HeuristicLab.Persistence.Default.Decomposers { 50 9 51 10 public class EnumerableDecomposer : IDecomposer { … … 68 27 } 69 28 29 public IEnumerable<Tag> CreateMetaInfo(object o) { 30 return new Tag[] { }; 31 } 32 70 33 public IEnumerable<Tag> Decompose(object obj) { 71 34 foreach (object o in (IEnumerable)obj) { … … 74 37 } 75 38 76 public object CreateInstance(Type type ) {39 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 77 40 return Activator.CreateInstance(type, true); 78 41 } 79 42 80 public objectPopulate(object instance, IEnumerable<Tag> tags, Type type) {43 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 81 44 MethodInfo addMethod = type.GetMethod("Add"); 82 EnumerableCache cache = new EnumerableCache(instance, addMethod); 83 foreach (var tag in tags) { 84 tag.SafeSet(cache.GetNextSetter()); 85 } 86 cache.Terminate(); 87 return instance; 45 foreach (var tag in tags) 46 addMethod.Invoke(instance, new[] { tag.Value }); 88 47 } 89 90 } 91 48 } 92 49 }
Note: See TracChangeset
for help on using the changeset viewer.