19 using Newtonsoft.Json;
21 using System.Reflection;
22 using Newtonsoft.Json.Linq;
23 using System.Linq.Expressions;
25 using System.Collections.Generic;
34 private static readonly MethodInfo SetPropertyMethodInfo = typeof(
Command).GetMethod(
"SetProperty");
35 private static readonly MethodInfo GetPropertyMethodInfo = typeof(
Command).GetMethod(
"GetProperty");
37 private readonly Dictionary<string, object> _storage =
new(StringComparer.InvariantCultureIgnoreCase);
47 public sealed
override DynamicMetaObject
GetMetaObject(Expression parameter)
49 return new SerializableDynamicMetaObject(parameter,
this, SetPropertyMethodInfo, GetPropertyMethodInfo);
60 if (value is JArray jArray)
62 return _storage[name] = jArray.ToObject<List<object>>();
64 else if (value is JObject jobject)
66 return _storage[name] = jobject.ToObject<Dictionary<string, object>>();
70 return _storage[name] = value;
81 if (!_storage.TryGetValue(name, out var value))
86 var propertyInfo = type.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
87 if (propertyInfo !=
null)
89 return propertyInfo.GetValue(
this,
null);
91 var fieldInfo = type.GetField(name, BindingFlags.Public | BindingFlags.Instance);
92 if (fieldInfo !=
null)
94 return fieldInfo.GetValue(
this);
109 throw new NotImplementedException($
"Please implement the 'def run(algorithm) -> bool | None:' method");
121 return JsonConvert.SerializeObject(
this);
129 private readonly
Command _object;
130 public SerializableDynamicMetaObject(Expression expression,
object value, MethodInfo setPropertyMethodInfo, MethodInfo getPropertyMethodInfo)
131 : base(expression, value, setPropertyMethodInfo, getPropertyMethodInfo)
135 public override IEnumerable<string> GetDynamicMemberNames()
137 return _object._storage.Keys.Concat(_object.GetType()
138 .GetMembers(BindingFlags.Public | BindingFlags.Instance)
139 .Where(x => x.MemberType == MemberTypes.Field || x.MemberType == MemberTypes.Property).Select(x => x.Name));