Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924_DotNetCoreMigration/HeuristicLab.DefinitionLanguage/PropertyDescription.cs @ 16985

Last change on this file since 16985 was 16985, checked in by dpiringe, 5 years ago

#2924:

  • added CLI Framework HeuristicLab.CommandLineInterface
  • added definition language test project HeuristicLab.DefinitionLanguage
  • added test project HeuristicLab.DynamicAssemblyTestApp, for PluginInfrastructure testing
  • changed project HeuristicLab to .NET Core and used it to create a CLI-Tool with the new CLI Framework
  • added Docker support to HeuristicLab
  • added IRunnerHost.cs ... forgot last commit
  • changed DockerRunnerHost and NativeRunnerHost to HeuristicLab-3.3.exe, was a little test project before
  • added new solution file HeuristicLab 3.3 No Views.sln, where all view projects are unloaded at start
File size: 3.2 KB
Line 
1using System;
2using System.Linq;
3using System.Reflection;
4using Newtonsoft.Json;
5using Newtonsoft.Json.Linq;
6using Newtonsoft.Json.Serialization;
7
8namespace HeuristicLab.DefinitionLanguage {
9  internal enum PropertyType : long {
10    Primitive,
11    Reference
12  }
13
14  [JsonConverter(typeof(BaseConverter))]
15  internal abstract class Property {
16    public string Key { get; set; }
17    public string PropertyType { get; set; }
18
19    public abstract void Set(object instance);
20
21    protected PropertyInfo GetInfo(object instance) {
22      var type = instance.GetType();
23      var all = type.GetProperties().Where(x => x.Name == Key);
24      return all.FirstOrDefault(x => x.DeclaringType == type) ?? all.First();
25    }
26  }
27
28  internal class PrimitiveProperty : Property {
29    public string Value { get; set; }
30    public PrimitiveProperty() {
31      PropertyType = "Primitive";
32    }
33    public override void Set(object instance) {
34      var info = GetInfo(instance);
35      info.SetValue(instance, Parser.ParseString(Value, info.PropertyType));
36    }
37  }
38
39  internal class ReferenceProperty : Property {
40    public string Value { get; set; }
41
42    public ReferenceProperty() {
43      PropertyType = "Reference";
44    }
45    public override void Set(object instance) {
46      var info = GetInfo(instance);
47      info.SetValue(instance, Parser.GetInstance(Value));
48    }
49  }
50
51  internal class ReferenceListProperty : Property {
52    public string[] ReferenceList { get; set; }
53
54    public override void Set(object instance) {
55      throw new NotImplementedException();
56    }
57  }
58
59  //https://stackoverflow.com/questions/20995865/deserializing-json-to-abstract-class
60  public class BaseSpecifiedConcreteClassConverter : DefaultContractResolver {
61    protected override JsonConverter ResolveContractConverter(Type objectType) {
62      if (typeof(Property).IsAssignableFrom(objectType) && !objectType.IsAbstract)
63        return null; // pretend TableSortRuleConvert is not specified (thus avoiding a stack overflow)
64      return base.ResolveContractConverter(objectType);
65    }
66  }
67
68  public class BaseConverter : JsonConverter {
69    static JsonSerializerSettings SpecifiedSubclassConversion = new JsonSerializerSettings() { ContractResolver = new BaseSpecifiedConcreteClassConverter() };
70
71    public override bool CanConvert(Type objectType) => objectType == typeof(Property);
72
73    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
74      JObject jo = JObject.Load(reader);
75      switch (jo["PropertyType"].Value<string>()) {
76        case "Primitive":
77          return JsonConvert.DeserializeObject<PrimitiveProperty>(jo.ToString(), SpecifiedSubclassConversion);
78        case "Reference":
79          return JsonConvert.DeserializeObject<ReferenceProperty>(jo.ToString(), SpecifiedSubclassConversion);
80        default:
81          throw new Exception();
82      }
83      throw new NotImplementedException();
84    }
85
86    public override bool CanWrite => false;
87
88    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
89      throw new NotImplementedException(); // won't be called because CanWrite returns false
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.