Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/14/09 13:23:08 (16 years ago)
Author:
epitzer
Message:

Replace final fixes for broken parent references with separation of instance creation with meta information. (#548)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs

    r1542 r1553  
    66using System.Collections.Generic;
    77
    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   }
     8namespace HeuristicLab.Persistence.Default.Decomposers {
    509
    5110  public class EnumerableDecomposer : IDecomposer {
     
    6827    }
    6928
     29    public IEnumerable<Tag> CreateMetaInfo(object o) {
     30      return new Tag[] { };
     31    }
     32
    7033    public IEnumerable<Tag> Decompose(object obj) {
    7134      foreach (object o in (IEnumerable)obj) {
     
    7437    }
    7538
    76     public object CreateInstance(Type type) {
     39    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    7740      return Activator.CreateInstance(type, true);
    7841    }
    7942
    80     public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
     43    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
    8144      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 });
    8847    }
    89 
    90   } 
    91 
     48  }
    9249}
Note: See TracChangeset for help on using the changeset viewer.