Advertisement
Guest User

Base class for base classes for Block Models

a guest
Jul 23rd, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 KB | None | 0 0
  1. namespace Root.Core.Models.Fields
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Diagnostics;
  6.     using System.Diagnostics.CodeAnalysis;
  7.     using System.Globalization;
  8.     using System.Linq;
  9.     using System.Reflection;
  10.     using Deliverystack.Core;
  11.     using Newtonsoft.Json;
  12.     using Newtonsoft.Json.Linq;
  13.  
  14.     //TODO: refactor to avoid the need for this class
  15.     public abstract class ModularBlockBase : Component
  16.     {
  17.         private static List<Type> _implementations = null;
  18.         private static object _sync = new object();
  19.  
  20.         public static Type[] Implementations
  21.         {
  22.             get
  23.             {
  24.                 if (_implementations == null)
  25.                 {
  26.                     lock (_sync)
  27.                     {
  28.                         _implementations = new List<Type>();
  29.  
  30.                         foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  31.                         {
  32.                             try
  33.                             {
  34.                                 foreach (Type type in assembly.GetExportedTypes())
  35.                                 {
  36.                                     if (type.BaseType != null
  37.                                         && type.BaseType.BaseType != null
  38.                                         && type.BaseType.BaseType == typeof(ModularBlockBase))
  39.                                     {
  40.                                         _implementations.Add(type);
  41.                                     }
  42.                                 }
  43.                             }
  44.                             catch (Exception ex)
  45.                             {
  46.                                 //TODO: assembly is obfuscated, etc.
  47.                                 new Instrument().Exception(ex, ex, "unable to load types from assembly {0}", assembly);
  48.                             }
  49.                         }
  50.                     }
  51.                 }
  52.  
  53.                 return _implementations.ToArray();
  54.             }
  55.         }
  56.     };
  57.    
  58.     public abstract class ModularBlockBase<TBlockBase, TBlockTypeEnum> : ModularBlockBase
  59.         where TBlockBase : class
  60.         where TBlockTypeEnum : struct, IConvertible
  61.     {
  62.         public class ModularBlocksJsonConverter : JsonConverter<TBlockBase>
  63.         {
  64.             public override TBlockBase ReadJson(
  65.                 JsonReader reader,
  66.                 Type objectType,
  67.                 TBlockBase existingValue,
  68.                 bool hasExistingValue,
  69.                 JsonSerializer serializer)
  70.             {
  71.                 TBlockTypeEnum parsed;
  72.                 JObject jObject = JObject.Load(reader);
  73.  
  74.                 if (Enum.TryParse(
  75.                     jObject.Children().First().Children().First().Path,
  76.                     true,
  77.                     out parsed))
  78.                 {
  79.                     foreach (Type t in Assembly.GetAssembly(
  80.                         typeof(TBlockBase)).GetTypes().Where(
  81.                             myType => myType.IsClass
  82.                                 && !myType.IsAbstract
  83.                                 && myType.IsSubclassOf(typeof(TBlockBase))))
  84.                     {
  85.                         TBlockBase obj = (TBlockBase)Activator.CreateInstance(t);
  86.  
  87.                         foreach (PropertyInfo propertyInfo in obj.GetType().GetRuntimeProperties())
  88.                         {
  89.                             if (propertyInfo.PropertyType == typeof(TBlockTypeEnum))
  90.                             {
  91.                                 if (((int)propertyInfo.GetValue(obj))
  92.                                     == parsed.ToInt32(CultureInfo.InvariantCulture))
  93.                                 {
  94.                                     serializer.Populate(jObject.GetValue(
  95.                                         parsed.ToString().ToLower()).CreateReader(), obj);
  96.                                     return obj;
  97.                                 }
  98.                             }
  99.                         }
  100.                     }
  101.                 }
  102.  
  103.                 Trace.Assert(
  104.                     false,
  105.                     "Unable to locate " + typeof(TBlockTypeEnum) + " property or matching "
  106.                     + parsed + " in classes of " + Assembly.GetAssembly(typeof(TBlockBase))
  107.                     + " that derive from " + typeof(TBlockBase));
  108.                 return null;
  109.             }
  110.  
  111.             public override void WriteJson(
  112.                 JsonWriter writer,
  113.                 [AllowNull] TBlockBase value,
  114.                 JsonSerializer serializer)
  115.             {
  116.                 throw new NotImplementedException();
  117.             }
  118.         }
  119.        
  120.         private static JsonConverter _converter = null;
  121.  
  122.         public JsonConverter GetJsonConverter()
  123.         {
  124.             if (_converter == null)
  125.             {
  126.                 _converter = new ModularBlocksJsonConverter();
  127.             }
  128.  
  129.             return _converter;
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement