Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1625 was 1623, checked in by epitzer, 16 years ago

Namespace refactoring, visibility check (#548)

File size: 1.6 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.Storable {
8
9  [EmptyStorableClass]
10  public class StorableDecomposer : IDecomposer {
11
12    public int Priority {
13      get { return 200; }
14    }
15
16    public bool CanDecompose(Type type) {
17      return StorableAttribute.GetStorableMembers(type, false).Count() > 0 ||
18        EmptyStorableClassAttribute.IsEmptyStorable(type);
19
20    }
21
22    public IEnumerable<Tag> CreateMetaInfo(object o) {
23      return new Tag[] { };
24    }
25
26    public IEnumerable<Tag> Decompose(object obj) {
27      foreach (var mapping in StorableAttribute.GetStorableAccessors(obj)) {
28        yield return new Tag(mapping.Key, mapping.Value.Get());
29      }
30    }
31
32    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
33      return Activator.CreateInstance(type, true);
34    }
35
36    public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
37      var memberDict = new Dictionary<string, Tag>();
38      IEnumerator<Tag> iter = objects.GetEnumerator();
39      while (iter.MoveNext()) {
40        memberDict.Add(iter.Current.Name, iter.Current);
41      }
42      foreach (var mapping in StorableAttribute.GetStorableAccessors(instance)) {
43        if (memberDict.ContainsKey(mapping.Key)) {
44          mapping.Value.Set(memberDict[mapping.Key].Value);
45        } else if (mapping.Value.DefaultValue != null) {
46          mapping.Value.Set(mapping.Value.DefaultValue);
47        }
48      }
49    }
50  }
51}
Note: See TracBrowser for help on using the repository browser.