/// <summary> /// Runs the conversion handler. /// </summary> /// <param name="ctx">The context.</param> /// <param name="type">The handler type to run.</param> internal virtual void Run(DittoConversionHandlerContext ctx, DittoConversionHandlerType type) { this.Content = ctx.Content; this.ModelType = ctx.ModelType; this.Model = ctx.Model; this.Run(type); }
/// <summary> /// Convenience method for calling converting/converter handlers. /// </summary> /// <typeparam name="TAttributeType">The type of the attribute type.</typeparam> /// <param name="conversionType">Type of the conversion.</param> /// <param name="content">The content.</param> /// <param name="type">The type.</param> /// <param name="culture">The culture.</param> /// <param name="instance">The instance.</param> /// <param name="callback">The callback.</param> private static void OnConvert <TAttributeType>( DittoConversionHandlerType conversionType, IPublishedContent content, Type type, CultureInfo culture, object instance, Action <DittoConversionHandlerContext> callback) where TAttributeType : Attribute { // Trigger conversion handlers var conversionCtx = new DittoConversionHandlerContext { Content = content, Culture = culture, ModelType = type, Model = instance }; // Check for class level DittoConversionHandlerAttribute foreach (var attr in type.GetCustomAttributes <DittoConversionHandlerAttribute>()) { ((DittoConversionHandler)attr.HandlerType.GetInstance()) .Run(conversionCtx, conversionType); } // Check for globaly registered handlers foreach (var handlerType in DittoConversionHandlerRegistry.Instance.GetRegisteredHandlerTypesFor(type)) { ((DittoConversionHandler)handlerType.GetInstance()) .Run(conversionCtx, conversionType); } // Check for method level DittoOnConvert[ing|ed]Attribute foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(x => x.GetCustomAttribute <TAttributeType>() != null)) { var p = method.GetParameters(); if (p.Length == 1 && p[0].ParameterType == typeof(DittoConversionHandlerContext)) { method.Invoke(instance, new object[] { conversionCtx }); } } // Check for a callback function if (callback != null) { callback(conversionCtx); } }
/// <summary>Convenience method for calling converting/converter handlers.</summary> /// <typeparam name="TAttributeType">The type of the attribute type.</typeparam> /// <param name="conversionType">Type of the conversion.</param> /// <param name="content">The content.</param> /// <param name="config">The Ditto configuration for the type.</param> /// <param name="culture">The culture.</param> /// <param name="instance">The instance.</param> /// <param name="callback">The callback.</param> private static void OnConvert <TAttributeType>( DittoConversionHandlerType conversionType, IPublishedContent content, DittoTypeInfo config, CultureInfo culture, object instance, Action <DittoConversionHandlerContext> callback) where TAttributeType : Attribute { // Trigger conversion handlers var conversionCtx = new DittoConversionHandlerContext { Content = content, Culture = culture, ModelType = config.TargetType, Model = instance }; // Run the registered handlers foreach (var handler in config.ConversionHandlers) { handler.Run(conversionCtx, conversionType); } var methods = conversionType == DittoConversionHandlerType.OnConverting ? config.ConvertingMethods : config.ConvertedMethods; if (methods.Any()) { foreach (var method in methods) { // TODO: Review this, `Invoke` could be CPU heavy?! method.Invoke(instance, new object[] { conversionCtx }); // Could we use a RuntimeMethodHandle? // https://web.archive.org/web/20150118044646/http://msdn.microsoft.com:80/en-us/magazine/cc163759.aspx#S8 } } // Check for a callback function callback?.Invoke(conversionCtx); }