Advertisement
commodore73

Recursive Set Properties on Entry Model and Block Models

Jul 23rd, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. private static void RecursiveSetProperties(
  2.     object outerObject,
  3.     Entry entry,
  4.     bool isFirst = false)
  5. {
  6.     if (ShouldSkip(outerObject.GetType()))
  7.     {
  8.         return;
  9.     }
  10.  
  11.     foreach (PropertyInfo outerPropInfo in
  12.         outerObject.GetType().GetRuntimeProperties())
  13.     {
  14.         if (outerPropInfo.PropertyType == typeof(JObject)
  15.             && outerPropInfo.Name == "JObject")
  16.         {
  17.             outerPropInfo.SetValue(outerObject, entry.ToJson());
  18.         }
  19.  
  20.         if (isFirst && outerPropInfo.PropertyType == typeof(String)
  21.             && outerPropInfo.Name == "ContentTypeUid")
  22.         {
  23.             outerPropInfo.SetValue(outerObject, entry.GetContentTypeWrapper().ContentTypeUid);
  24.         }
  25.  
  26.         if (ShouldSkip(outerPropInfo.PropertyType))
  27.         {
  28.             continue;
  29.         }
  30.  
  31.         if (outerPropInfo.Name == "Entry" && outerPropInfo.PropertyType == typeof(Entry))
  32.         {
  33.             outerPropInfo.SetValue(outerObject, entry);
  34.         }
  35.         else if (outerPropInfo.Name == "Repository"
  36.             && outerPropInfo.PropertyType == typeof(IRepository))
  37.         {
  38.             // entry.GetRepository() implementation DOES NOT access Repository,
  39.             // which could otherwise cause infinite recursion if outerObject is Entry?
  40.             outerPropInfo.SetValue(outerObject, entry.GetRepository());
  41.         }
  42.         else
  43.         {
  44.             ICollection collection = outerPropInfo.GetValue(outerObject)
  45.                 as ICollection;
  46.  
  47.             if (collection != null)
  48.             {
  49.                 foreach (object thing in collection)
  50.                 {
  51.                     RecursiveSetProperties(thing, entry);
  52.                 }
  53.             }
  54.             else
  55.             {
  56.                 try
  57.                 {
  58.                     Object innerObject = outerPropInfo.GetValue(outerObject);
  59.  
  60.                     if (innerObject != null)
  61.                     {
  62.                         RecursiveSetProperties(innerObject, entry);
  63.                     }
  64.                 }
  65.                 catch (TargetParameterCountException ex)
  66.                 {
  67.                     entry.GetRepository().Instrument.Exception(
  68.                         outerObject,
  69.                         ex,
  70.                         "unable to access {0}  of {1}",
  71.                         outerPropInfo.Name,
  72.                         outerObject);
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. private static bool ShouldSkip(Type t)
  80. {
  81.     return t.IsValueType
  82.         || t == typeof(Object)
  83.         || t == typeof(String)
  84.         || t == typeof(JArray);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement