Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void RecursiveSetProperties(
- object outerObject,
- Entry entry,
- bool isFirst = false)
- {
- if (ShouldSkip(outerObject.GetType()))
- {
- return;
- }
- foreach (PropertyInfo outerPropInfo in
- outerObject.GetType().GetRuntimeProperties())
- {
- if (outerPropInfo.PropertyType == typeof(JObject)
- && outerPropInfo.Name == "JObject")
- {
- outerPropInfo.SetValue(outerObject, entry.ToJson());
- }
- if (isFirst && outerPropInfo.PropertyType == typeof(String)
- && outerPropInfo.Name == "ContentTypeUid")
- {
- outerPropInfo.SetValue(outerObject, entry.GetContentTypeWrapper().ContentTypeUid);
- }
- if (ShouldSkip(outerPropInfo.PropertyType))
- {
- continue;
- }
- if (outerPropInfo.Name == "Entry" && outerPropInfo.PropertyType == typeof(Entry))
- {
- outerPropInfo.SetValue(outerObject, entry);
- }
- else if (outerPropInfo.Name == "Repository"
- && outerPropInfo.PropertyType == typeof(IRepository))
- {
- // entry.GetRepository() implementation DOES NOT access Repository,
- // which could otherwise cause infinite recursion if outerObject is Entry?
- outerPropInfo.SetValue(outerObject, entry.GetRepository());
- }
- else
- {
- ICollection collection = outerPropInfo.GetValue(outerObject)
- as ICollection;
- if (collection != null)
- {
- foreach (object thing in collection)
- {
- RecursiveSetProperties(thing, entry);
- }
- }
- else
- {
- try
- {
- Object innerObject = outerPropInfo.GetValue(outerObject);
- if (innerObject != null)
- {
- RecursiveSetProperties(innerObject, entry);
- }
- }
- catch (TargetParameterCountException ex)
- {
- entry.GetRepository().Instrument.Exception(
- outerObject,
- ex,
- "unable to access {0} of {1}",
- outerPropInfo.Name,
- outerObject);
- }
- }
- }
- }
- }
- private static bool ShouldSkip(Type t)
- {
- return t.IsValueType
- || t == typeof(Object)
- || t == typeof(String)
- || t == typeof(JArray);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement