Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/StorableDecomposer.cs @ 1539

Last change on this file since 1539 was 1539, checked in by epitzer, 15 years ago

Add Priority property to IDecomposer interface to allow specialized decomposers to be tried first. (#578)

File size: 1.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Persistence.Interfaces;
5using HeuristicLab.Persistence.Core;
6
7namespace HeuristicLab.Persistence.Default.Decomposers {
8
9  public class StorableDecomposer : IDecomposer {
10
11    public int Priority {
12      get { return 200; }
13    }
14
15    public bool CanDecompose(Type type) {
16      return StorableAttribute.GetStorableMembers(type, false).Count() > 0 ||
17        EmptyStorableClassAttribute.IsEmpyStorable(type);
18
19    }   
20
21    public IEnumerable<Tag> DeCompose(object obj) {
22      foreach (var mapping in StorableAttribute.GetAutostorableAccessors(obj)) {
23        yield return new Tag(mapping.Key, mapping.Value.Get());
24      }
25    }
26
27    public object CreateInstance(Type type) {
28      return Activator.CreateInstance(type, true);
29    }
30
31    public object Populate(object instance, IEnumerable<Tag> objects, Type type) {           
32      var memberDict = new Dictionary<string, Tag>();     
33      IEnumerator<Tag> iter = objects.GetEnumerator();     
34      while (iter.MoveNext()) {
35        memberDict.Add(iter.Current.Name, iter.Current);
36      }     
37      foreach (var mapping in StorableAttribute.GetAutostorableAccessors(instance)) {
38        if (memberDict.ContainsKey(mapping.Key)) {
39          memberDict[mapping.Key].SafeSet(mapping.Value.Set);
40        } else if (mapping.Value.DefaultValue != null) {
41          mapping.Value.Set(mapping.Value.DefaultValue);
42        }
43      }
44      return instance;
45    }
46
47  }
48}
Note: See TracBrowser for help on using the repository browser.