Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/StorableConverter.cs @ 17353

Last change on this file since 17353 was 17353, checked in by dpiringe, 4 years ago

#3026:

  • relocated GetMaxValue and GetMinValue from ValueTypeValueConverter into BaseConverter
  • fixed a bug in ConstrainedValueParameterConverter (from GetType().Name to ToString())
  • printing now PrettyNames for types
  • added comments
  • added StorableConverter.cs (not finished, maybe not a good converter)
  • added ValueRangeConverter.cs for DoubleRange and IntRange
  • added ParameterConverter.cs for default parameter conversion
File size: 2.6 KB
Line 
1using System;
2using HEAL.Attic;
3using System.Collections.Generic;
4using HeuristicLab.Common;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using HeuristicLab.Core;
9using System.Reflection;
10using HeuristicLab.Data;
11using System.Collections;
12
13namespace HeuristicLab.JsonInterface {
14
15
16  public class StorableConverter : BaseConverter {
17    private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
18
19    private bool ContainsOnlyPrimitives(IEnumerable enumerable) {
20     
21      foreach (var i in enumerable) {
22        if (!i.GetType().IsPrimitive) return false;
23      }
24      return true;
25    }
26
27    private void ExtractFromMemberInfo(MemberInfo info, object obj, JsonItem item) {
28      string name = (obj is IItem) ? ((IItem)obj).ItemName : info.Name;
29      if (StorableAttribute.IsStorable(info)) {
30        object tmp = GetValue(info, obj);
31        if (tmp is IItem)
32          item.Parameters.Add(JsonItemConverter.Extract((IItem)tmp));
33        else if (tmp is IDictionary) {
34        }
35        else if(tmp is IEnumerable) {
36          IEnumerable c = (IEnumerable)tmp;
37          List<object> objs = new List<object>();
38          foreach (var i in c) {
39            if (i is IItem)
40              item.Parameters.Add(JsonItemConverter.Extract((IItem)i));
41            else
42              objs.Add(i);
43          }
44          item.Value = objs;
45        }
46      }
47    }
48
49    public override JsonItem ExtractData(IItem value) {
50      Type type = value.GetType();
51
52      JsonItem item = new JsonItem() {
53        Name = value.ItemName,
54        Path = value.ItemName,
55        Value = ".",
56        Type = value.GetType().AssemblyQualifiedName
57      };
58      item.Parameters = new List<JsonItem>();
59
60     
61      do {
62        foreach (var property in type.GetProperties(flags)) {
63          ExtractFromMemberInfo(property, value, item);
64        }
65
66        foreach (var field in type.GetFields(flags)) {
67          ExtractFromMemberInfo(field, value, item);
68        }
69
70        type = type.BaseType;
71      } while (type != null);
72
73
74      return item;
75    }
76
77    public override void InjectData(IItem item, JsonItem data) {
78      throw new NotImplementedException();
79    }
80
81
82    private object GetValue(MemberInfo info, object obj) {
83      switch(info.MemberType) {
84        case MemberTypes.Field:
85          return ((FieldInfo)info).GetValue(obj);
86        case MemberTypes.Property:
87          return ((PropertyInfo)info).CanRead ? ((PropertyInfo)info).GetValue(obj) : null;
88        default: return null;
89      }
90    }     
91  }
92}
Note: See TracBrowser for help on using the repository browser.