private static void PopulateModel(ContentViewModel model, IPublishedContent content) { var type = model.GetType(); var properties = new Dictionary <string, PropertyInfo>(StringComparer.OrdinalIgnoreCase); foreach (var property in type.GetProperties()) { if (!property.CanWrite) { continue; } if (properties.ContainsKey(property.Name)) { continue; } properties.Add(property.Name, property); } foreach (var field in content.Properties) { PropertyInfo property; if (!properties.TryGetValue(field.PropertyTypeAlias, out property)) { continue; } if (field.Value == null) { continue; } var fieldType = field.Value.GetType(); if (!property.PropertyType.IsAssignableFrom(fieldType)) { continue; } property.SetValue(model, field.Value); } }
internal static ContentViewModel GetViewModel(IPublishedContent content) { ContentViewModel model = new ContentViewModel(content); Type type; if (CandidateClasses.TryGetValue(content.DocumentTypeAlias, out type)) { model = Activator.CreateInstance(type, content) as ContentViewModel; } if (model == null) { return(model); } PopulateModel(model, content); return(model); }