Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/EnumTypeConverter.cs @ 17828

Last change on this file since 17828 was 17828, checked in by dpiringe, 3 years ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 1.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8
9namespace HeuristicLab.JsonInterface {
10  public class EnumTypeConverter : BaseConverter {
11    public override int Priority => 1;
12    public override Type ConvertableType => typeof(EnumValue<>);
13
14    public override bool CanConvertType(Type t) =>
15      typeof(EnumValue<>).IsAssignableFrom(t) ||
16      (t.IsGenericType && t.GetGenericTypeDefinition() == ConvertableType);
17
18    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
19      ((dynamic)item).Value = Enum.Parse(
20        item.GetType().GenericTypeArguments.First(),
21        ((StringJsonItem)data).Value);
22   
23    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
24      object val = ((dynamic)value).Value;
25      Type enumType = val.GetType();
26      return new StringJsonItem() {
27        Name = value.ItemName,
28        Description = value.ItemDescription,
29        Value = Enum.GetName(enumType, val),
30        ConcreteRestrictedItems = Enum.GetNames(enumType)
31      };
32    }
33  }
34}
Note: See TracBrowser for help on using the repository browser.