Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • added property Description in IJsonItem and updated all construction calls
  • updated UnsupportedJsonItem with unsupported property Description
  • updated JsonItemBaseControl and JsonItemVMBase for new property Description
File size: 998 bytes
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 void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
15      ((dynamic)item).Value = Enum.Parse(
16        item.GetType().GenericTypeArguments.First(),
17        ((StringJsonItem)data).Value);
18   
19    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
20      object val = ((dynamic)value).Value;
21      Type enumType = val.GetType();
22      return new StringJsonItem() {
23        Name = value.ItemName,
24        Description = value.ItemDescription,
25        Value = Enum.GetName(enumType, val),
26        Range = Enum.GetNames(enumType)
27    };
28    }
29  }
30}
Note: See TracBrowser for help on using the repository browser.