[1623] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 5 | using HeuristicLab.Persistence.Core;
|
---|
[1705] | 6 | using System.Reflection;
|
---|
| 7 | using HeuristicLab.Persistence.Auxiliary;
|
---|
[1623] | 8 |
|
---|
[1823] | 9 | namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
|
---|
[1623] | 10 |
|
---|
| 11 | [EmptyStorableClass]
|
---|
[1823] | 12 | public class StorableSerializer : ICompositeSerializer {
|
---|
[1623] | 13 |
|
---|
| 14 | public int Priority {
|
---|
| 15 | get { return 200; }
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[1823] | 18 | public bool CanSerialize(Type type) {
|
---|
[1852] | 19 | if (!ReflectionTools.HasDefaultConstructor(type))
|
---|
| 20 | return false;
|
---|
| 21 | while (type != null) {
|
---|
| 22 | if (StorableAttribute.GetStorableMembers(type, false).Count() == 0 &&
|
---|
| 23 | !EmptyStorableClassAttribute.IsEmptyStorable(type))
|
---|
| 24 | return false;
|
---|
| 25 | type = type.BaseType;
|
---|
| 26 | }
|
---|
| 27 | return true;
|
---|
[1623] | 28 | }
|
---|
| 29 |
|
---|
| 30 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
[2980] | 31 | StorableHookAttribute.InvokeHook(HookType.BeforeSerialization, o);
|
---|
[1623] | 32 | return new Tag[] { };
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
[1960] | 36 | foreach (var accessor in StorableAttribute.GetStorableAccessors(obj)) {
|
---|
| 37 | yield return new Tag(accessor.Name, accessor.Get());
|
---|
[1623] | 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
[2990] | 42 | try {
|
---|
| 43 | object instance = StorableConstructorAttribute.CallStorableConstructor(type);
|
---|
| 44 | if (instance == null)
|
---|
| 45 | instance = Activator.CreateInstance(type, true);
|
---|
| 46 | return instance;
|
---|
| 47 | } catch (TargetInvocationException x) {
|
---|
| 48 | throw new PersistenceException(
|
---|
| 49 | "Could not instantiate storable object: Encountered exception during constructor call",
|
---|
| 50 | x.InnerException);
|
---|
| 51 | }
|
---|
[1623] | 52 | }
|
---|
| 53 |
|
---|
| 54 | public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
|
---|
| 55 | var memberDict = new Dictionary<string, Tag>();
|
---|
| 56 | IEnumerator<Tag> iter = objects.GetEnumerator();
|
---|
| 57 | while (iter.MoveNext()) {
|
---|
| 58 | memberDict.Add(iter.Current.Name, iter.Current);
|
---|
| 59 | }
|
---|
[2980] | 60 | foreach (var accessor in StorableAttribute.GetStorableAccessors(instance)) {
|
---|
[1960] | 61 | if (memberDict.ContainsKey(accessor.Name)) {
|
---|
[2980] | 62 | accessor.Set(memberDict[accessor.Name].Value);
|
---|
[1960] | 63 | } else if (accessor.DefaultValue != null) {
|
---|
[2980] | 64 | accessor.Set(accessor.DefaultValue);
|
---|
[1623] | 65 | }
|
---|
| 66 | }
|
---|
[2980] | 67 | StorableHookAttribute.InvokeHook(HookType.AfterDeserialization, instance);
|
---|
[1623] | 68 | }
|
---|
| 69 | }
|
---|
[2980] | 70 | } |
---|