/// <summary> /// Calls any methods designed to handle <see cref="ConvertedTypeEventArgs"/> events. /// </summary> /// <param name="args"> /// The <see cref="ConvertedTypeEventArgs"/>. /// </param> public static void CallConvertedTypeHandler(ConvertedTypeEventArgs args) { if (ConvertedType != null) { ConvertedType(null, args); } }
/// <summary> /// Returns an object representing the given <see cref="Type"/>. /// </summary> /// <param name="content"> /// The <see cref="IPublishedContent"/> to convert. /// </param> /// <param name="type"> /// The <see cref="Type"/> of items to return. /// </param> /// <param name="convertingType"> /// The <see cref="Action{ConvertingTypeEventArgs}"/> to fire when converting. /// </param> /// <param name="convertedType"> /// The <see cref="Action{ConvertedTypeEventArgs}"/> to fire when converted. /// </param> /// <returns> /// The converted <see cref="Object"/> as the given type. /// </returns> public static object As( this IPublishedContent content, Type type, Action <ConvertingTypeEventArgs> convertingType = null, Action <ConvertedTypeEventArgs> convertedType = null, CultureInfo culture = null) { if (content == null) { return(null); } using (DisposableTimer.DebugDuration(type, string.Format("IPublishedContent As ({0})", content.DocumentTypeAlias), "Complete")) { // Check for and fire any event args var convertingArgs = new ConvertingTypeEventArgs { Content = content }; EventHandlers.CallConvertingTypeHandler(convertingArgs); if (!convertingArgs.Cancel && convertingType != null) { convertingType(convertingArgs); } // Cancel if applicable. if (convertingArgs.Cancel) { return(null); } // Create an object and fetch it as the type. object instance = GetTypedProperty(content, type, culture); // Fire the converted event var convertedArgs = new ConvertedTypeEventArgs { Content = content, Converted = instance, ConvertedType = type }; if (convertedType != null) { convertedType(convertedArgs); } EventHandlers.CallConvertedTypeHandler(convertedArgs); return(convertedArgs.Converted); } }
public static T As <T>(this IPublishedContent content) where T : class { using (var t = DisposableTimer.DebugDuration <T>(string.Format("As ({0})", content.DocumentTypeAlias))) { var type = typeof(T); T instance; var constructor = type.GetConstructors() .OrderBy(x => x.GetParameters().Length) .First(); var constructorParams = constructor.GetParameters(); var args1 = new ConvertingTypeEventArgs { Content = content }; CallConvertingTypeHandler(args1); if (args1.Cancel) { return(null); } if (constructorParams.Length == 0) { instance = Activator.CreateInstance(type) as T; } else if (constructorParams.Length == 1 & constructorParams[0].ParameterType == typeof(IPublishedContent)) { instance = Activator.CreateInstance(type, content) as T; } else { throw new InvalidOperationException("Type {0} has invalid constructor parameters"); } var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var contentType = content.GetType(); foreach (var propertyInfo in properties.Where(x => x.CanWrite)) { using (var propertyLoopTimer = DisposableTimer.DebugDuration <T>(string.Format("ForEach Property ({1} {0})", propertyInfo.Name, content.Id))) { var umbracoPropertyName = propertyInfo.Name; var altUmbracoPropertyName = ""; var recursive = false; object defaultValue = null; var umbracoPropertyAttr = propertyInfo.GetCustomAttribute <UmbracoPropertyAttribute>(); if (umbracoPropertyAttr != null) { umbracoPropertyName = umbracoPropertyAttr.PropertyName; altUmbracoPropertyName = umbracoPropertyAttr.AltPropertyName; recursive = umbracoPropertyAttr.Recursive; defaultValue = umbracoPropertyAttr.DefaultValue; } // Try fetching the value var contentProperty = contentType.GetProperty(umbracoPropertyName); var propertyValue = contentProperty != null ? contentProperty.GetValue(content, null) : content.GetPropertyValue(umbracoPropertyName, recursive); // Try fetching the alt value if (propertyValue == null && !string.IsNullOrWhiteSpace(altUmbracoPropertyName)) { contentProperty = contentType.GetProperty(altUmbracoPropertyName); propertyValue = contentProperty != null ? contentProperty.GetValue(content, null) : content.GetPropertyValue(altUmbracoPropertyName, recursive); } // Try setting the default value if (propertyValue == null && defaultValue != null) { propertyValue = defaultValue; } // Process the value if (propertyValue != null) { if (propertyInfo.PropertyType.IsInstanceOfType(propertyValue)) { propertyInfo.SetValue(instance, propertyValue, null); } else { using (var typeConverterTimer = DisposableTimer.DebugDuration <T>(string.Format("TypeConverter ({1} {0})", propertyInfo.Name, content.Id))) { var converterAttr = propertyInfo.GetCustomAttribute <TypeConverterAttribute>(); if (converterAttr != null) { var converter = Activator.CreateInstance(Type.GetType(converterAttr.ConverterTypeName)) as TypeConverter; propertyInfo.SetValue(instance, converter.ConvertFrom(propertyValue), null); } else { var convert = propertyValue.TryConvertTo(propertyInfo.PropertyType); if (convert.Success) { propertyInfo.SetValue(instance, convert.Result, null); } } } } } } } var args2 = new ConvertedTypeEventArgs { Content = content, Converted = instance, ConvertedType = typeof(T) }; CallConvertedTypeHandler(args2); return(args2.Converted as T); } }