Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Root.Core.Models.Fields
- {
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Diagnostics.CodeAnalysis;
- using System.Globalization;
- using System.Linq;
- using System.Reflection;
- using Deliverystack.Core;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- //TODO: refactor to avoid the need for this class
- public abstract class ModularBlockBase : Component
- {
- private static List<Type> _implementations = null;
- private static object _sync = new object();
- public static Type[] Implementations
- {
- get
- {
- if (_implementations == null)
- {
- lock (_sync)
- {
- _implementations = new List<Type>();
- foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
- {
- try
- {
- foreach (Type type in assembly.GetExportedTypes())
- {
- if (type.BaseType != null
- && type.BaseType.BaseType != null
- && type.BaseType.BaseType == typeof(ModularBlockBase))
- {
- _implementations.Add(type);
- }
- }
- }
- catch (Exception ex)
- {
- //TODO: assembly is obfuscated, etc.
- new Instrument().Exception(ex, ex, "unable to load types from assembly {0}", assembly);
- }
- }
- }
- }
- return _implementations.ToArray();
- }
- }
- };
- public abstract class ModularBlockBase<TBlockBase, TBlockTypeEnum> : ModularBlockBase
- where TBlockBase : class
- where TBlockTypeEnum : struct, IConvertible
- {
- public class ModularBlocksJsonConverter : JsonConverter<TBlockBase>
- {
- public override TBlockBase ReadJson(
- JsonReader reader,
- Type objectType,
- TBlockBase existingValue,
- bool hasExistingValue,
- JsonSerializer serializer)
- {
- TBlockTypeEnum parsed;
- JObject jObject = JObject.Load(reader);
- if (Enum.TryParse(
- jObject.Children().First().Children().First().Path,
- true,
- out parsed))
- {
- foreach (Type t in Assembly.GetAssembly(
- typeof(TBlockBase)).GetTypes().Where(
- myType => myType.IsClass
- && !myType.IsAbstract
- && myType.IsSubclassOf(typeof(TBlockBase))))
- {
- TBlockBase obj = (TBlockBase)Activator.CreateInstance(t);
- foreach (PropertyInfo propertyInfo in obj.GetType().GetRuntimeProperties())
- {
- if (propertyInfo.PropertyType == typeof(TBlockTypeEnum))
- {
- if (((int)propertyInfo.GetValue(obj))
- == parsed.ToInt32(CultureInfo.InvariantCulture))
- {
- serializer.Populate(jObject.GetValue(
- parsed.ToString().ToLower()).CreateReader(), obj);
- return obj;
- }
- }
- }
- }
- }
- Trace.Assert(
- false,
- "Unable to locate " + typeof(TBlockTypeEnum) + " property or matching "
- + parsed + " in classes of " + Assembly.GetAssembly(typeof(TBlockBase))
- + " that derive from " + typeof(TBlockBase));
- return null;
- }
- public override void WriteJson(
- JsonWriter writer,
- [AllowNull] TBlockBase value,
- JsonSerializer serializer)
- {
- throw new NotImplementedException();
- }
- }
- private static JsonConverter _converter = null;
- public JsonConverter GetJsonConverter()
- {
- if (_converter == null)
- {
- _converter = new ModularBlocksJsonConverter();
- }
- return _converter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement