/// <summary> /// Fills out class properties based on a provided, instantiated class and a Func instructing it how to get the raw property data based on alias /// </summary> /// <typeparam name="T">Type of instantiated class</typeparam> /// <param name="instance">An newed-up instance of T</param> /// <param name="getPropertyValue">A func that, provided a property alias, a PropertyInfo, and a recursion indicator, will return a raw value to be processed by the TypeHandler system</param> public void FillClassProperties <T>(T instance, Func <string, PropertyInfo, bool, object> getPropertyValue) { var properties = ClassConstructor.GetPropertiesToFill <T>(); foreach (var propertyInfo in properties) { object value; if (TryGetValueForProperty(getPropertyValue, propertyInfo, out value)) { propertyInfo.SetValue(instance, value, null); } } }
protected T GetMemberItem <T>(IMember m) { var result = ClassConstructor.CreateWithMember <T>(m); FillClassProperties(result, (alias, recursive) => { if (!m.HasProperty(alias)) { return(null); } var value = m.GetValue(alias); return(value); }); return(result); }
protected T GetItem <T>(IPublishedContent n) { var cachedItem = _cacheManager.GetItem <T>(n.Id); if (cachedItem != null) { return((T)cachedItem); } var result = ClassConstructor.CreateWithNode <T>(n); FillClassProperties(result, (alias, recursive) => { var value = n.GetPropertyValue(alias, recursive); return(value); }); _cacheManager.AddItem(n.Id, result); return(result); }
protected T GetItem <T>(IContent n) { var cachedItem = _cacheManager.GetItem <T>(n.Id); if (cachedItem != null) { return((T)cachedItem); } var result = ClassConstructor.CreateWithContent <T>(n); FillClassProperties(result, (alias, propertyInfo, recursive) => { var targetType = propertyInfo.PropertyType; var containsProperty = n.HasProperty(alias); if (containsProperty) { var property = n.Properties.First(p => string.Equals(p.Alias, alias, StringComparison.InvariantCultureIgnoreCase)); // the IPublishedNode.GetPropertyValue() will fetch the prevalue of the numeric item if // the target is a string. This emulates that functionality. if (targetType == typeof(string) && property.Value.IsNumeric()) { var name = n.GetPrevalues(ApplicationContext.Current.Services.DataTypeService, alias); return(name); } return(property.Value); } return(null); }); _cacheManager.AddItem(n.Id, result); return(result); }