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

Last change on this file since 18057 was 18057, checked in by dpiringe, 8 months ago

#3026

  • fixed a bug with dynamic casting in EnumTypeConverter
  • reworked RegressionProblemDataConverter
    • the target type is now: ValueParameter<IRegressionProblemData>
    • removed all dynamic/reflection code
    • reduced a lot of complexity
    • now it works with different variables (the Variable symbol bug should be fixed)
File size: 1.0 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5
6namespace HeuristicLab.JsonInterface {
7  public class EnumTypeConverter : BaseConverter {
8    public override int Priority => 1;
9
10    public override bool CanConvertType(Type t) =>
11      typeof(EnumValue<>).IsAssignableFrom(t) ||
12      (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(EnumValue<>));
13
14    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
15      ((dynamic)item).Value = (dynamic)Enum.Parse(
16        item.GetType().GenericTypeArguments.First(),
17        ((StringJsonItem)data).Value);
18    }
19     
20   
21    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
22      object val = ((dynamic)value).Value;
23      Type enumType = val.GetType();
24      return new StringJsonItem() {
25        Name = value.ItemName,
26        Description = value.ItemDescription,
27        Value = Enum.GetName(enumType, val),
28        ConcreteRestrictedItems = Enum.GetNames(enumType)
29      };
30    }
31  }
32}
Note: See TracBrowser for help on using the repository browser.