示例#1
0
        /// <summary>
        /// 执行形状。
        /// </summary>
        /// <param name="shape">形状。</param>
        /// <returns>形状结果。</returns>
        public object ShapeExecute(object shape)
        {
            if (shape == null)
            {
                return(new HtmlString(string.Empty));
            }

            var context = new DisplayContext {
                Display = this, Value = shape, ViewContext = ViewContext, ViewDataContainer = ViewDataContainer
            };

            return(_displayManager.Execute(context));
        }
        private IHtmlString PerformInvoke(DisplayContext displayContext, MethodInfo methodInfo, object serviceInstance)
        {
            var output = new HtmlStringWriter();
            var arguments = methodInfo.GetParameters()
                .Select(parameter => BindParameter(displayContext, parameter, output));

            var returnValue = methodInfo.Invoke(serviceInstance, arguments.ToArray());
            if (methodInfo.ReturnType != typeof(void))
            {
                output.Write(CoerceHtmlString(returnValue));
            }
            return output;
        }
        private object BindParameter(DisplayContext displayContext, ParameterInfo parameter, TextWriter output)
        {
            if (parameter.Name == "Shape")
                return displayContext.Value;

            if (parameter.Name == "Display")
                return displayContext.Display;

            if (parameter.Name == "Output" && parameter.ParameterType == typeof(TextWriter))
                return output;

            if (parameter.Name == "Output" && parameter.ParameterType == typeof(Action<object>))
                return new Action<object>(output.Write);

            if (parameter.Name == "Html")
            {
                return new HtmlHelper(
                    displayContext.ViewContext,
                    displayContext.ViewDataContainer,
                    _routeCollection);
            }

            if (parameter.Name == "Url" && parameter.ParameterType.IsAssignableFrom(typeof(UrlHelper)))
            {
                return new UrlHelper(displayContext.ViewContext.RequestContext, _routeCollection);
            }

            var getter = Getters.GetOrAdd(parameter.Name, n =>
                CallSite<Func<CallSite, object, dynamic>>.Create(
                Microsoft.CSharp.RuntimeBinder.Binder.GetMember(
                CSharpBinderFlags.None, n, null, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) })));

            var result = getter.Target(getter, displayContext.Value);

            if (result == null)
                return null;

            var converter = Converters.GetOrAdd(parameter.ParameterType, CompileConverter);
            var argument = converter.Invoke(result);
            return argument;
        }
        public IHtmlString Execute(DisplayContext context)
        {
            var shape = ConvertAsShapeCallsite.Target(ConvertAsShapeCallsite, context.Value);

            if (shape == null)
                return CoerceHtmlString(context.Value);

            var shapeMetadata = shape.Metadata;
            if (shapeMetadata == null || string.IsNullOrEmpty(shapeMetadata.Type))
                return CoerceHtmlString(context.Value);

            var workContext = _workContextAccessor.GetContext();
            var shapeTable = _httpContextAccessor.Current() != null
                ? _shapeTableLocator.Value.Lookup(workContext.GetCurrentTheme().Id)
                : _shapeTableLocator.Value.Lookup(null);

            var displayingContext = new ShapeDisplayingContext
            {
                Shape = shape,
                ShapeMetadata = shapeMetadata
            };
            _shapeDisplayEvents.Invoke(sde => sde.Displaying(displayingContext), Logger);

            ShapeBinding shapeBinding;
            if (TryGetDescriptorBinding(shapeMetadata.Type, Enumerable.Empty<string>(), shapeTable, out shapeBinding))
            {
                shapeBinding.ShapeDescriptor.Displaying.Invoke(action => action(displayingContext), Logger);

                shapeMetadata.BindingSources = shapeBinding.ShapeDescriptor.BindingSources.Where(x => x != null).ToList();
                if (!shapeMetadata.BindingSources.Any())
                {
                    shapeMetadata.BindingSources.Add(shapeBinding.ShapeDescriptor.BindingSource);
                }
            }

            shapeMetadata.Displaying.Invoke(action => action(displayingContext), Logger);

            if (displayingContext.ChildContent != null)
            {
                shape.Metadata.ChildContent = displayingContext.ChildContent;
            }
            else
            {
                ShapeBinding actualBinding;
                if (TryGetDescriptorBinding(shapeMetadata.Type, shapeMetadata.Alternates, shapeTable, out actualBinding))
                {
                    shape.Metadata.ChildContent = Process(actualBinding, shape, context);
                }
                else
                {
                    throw new RabbitException(T("找不到形状类型 {0}", shapeMetadata.Type));
                }
            }

            foreach (var frameType in shape.Metadata.Wrappers)
            {
                ShapeBinding frameBinding;
                if (TryGetDescriptorBinding(frameType, Enumerable.Empty<string>(), shapeTable, out frameBinding))
                {
                    shape.Metadata.ChildContent = Process(frameBinding, shape, context);
                }
            }

            var displayedContext = new ShapeDisplayedContext
            {
                Shape = shape,
                ShapeMetadata = shape.Metadata,
                ChildContent = shape.Metadata.ChildContent,
            };

            _shapeDisplayEvents.Invoke(sde =>
            {
                var prior = displayedContext.ChildContent = displayedContext.ShapeMetadata.ChildContent;
                sde.Displayed(displayedContext);
                if (prior != displayedContext.ChildContent)
                    displayedContext.ShapeMetadata.ChildContent = displayedContext.ChildContent;
            }, Logger);

            if (shapeBinding != null)
            {
                shapeBinding.ShapeDescriptor.Displayed.Invoke(action =>
                {
                    var prior = displayedContext.ChildContent = displayedContext.ShapeMetadata.ChildContent;
                    action(displayedContext);
                    if (prior != displayedContext.ChildContent)
                        displayedContext.ShapeMetadata.ChildContent = displayedContext.ChildContent;
                }, Logger);
            }

            shapeMetadata.Displayed.Invoke(action => action(displayedContext), Logger);

            return shape.Metadata.ChildContent;
        }
 private static IHtmlString Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context)
 {
     if (shapeBinding == null || shapeBinding.Binding == null)
     {
         return shape.Metadata.ChildContent ?? new HtmlString(string.Empty);
     }
     return CoerceHtmlString(shapeBinding.Binding(context));
 }
        private IHtmlString RenderRazorViewToString(string path, DisplayContext context)
        {
            using (var sw = new StringWriter())
            {
                var controllerContext = CreateControllerContext();
                var viewResult = _viewEngine.Value.FindPartialView(controllerContext, path, false);

                context.ViewContext.ViewData = new ViewDataDictionary(context.Value);
                context.ViewContext.TempData = new TempDataDictionary();
                viewResult.View.Render(context.ViewContext, sw);
                viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
                return new HtmlString(sw.GetStringBuilder().ToString());
            }
        }
        private IHtmlString Render(DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo)
        {
            Logger.Information("渲染模板文件 '{0}'", harvestShapeInfo.TemplateVirtualPath);
            IHtmlString result;

            if (displayContext.ViewContext.View != null)
            {
                var htmlHelper = new HtmlHelper(displayContext.ViewContext, displayContext.ViewDataContainer);
                result = htmlHelper.Partial(harvestShapeInfo.TemplateVirtualPath, displayContext.Value);
            }
            else
            {
                //如果视图为空,则表示该造型是从一个非视图产地/在没有的ViewContext成立了由视图引擎,但是手动执行。
                //手动创建的ViewContext工程与形状的方法工作时,而不是当形状被实现为一个Razor视图模板。
                //可怕的,但它会做现在。
                result = RenderRazorViewToString(harvestShapeInfo.TemplateVirtualPath, displayContext);
            }

            Logger.Information("完成目标文件 '{0}' 的渲染。", harvestShapeInfo.TemplateVirtualPath);
            return result;
        }
示例#8
0
        /// <summary>
        /// 执行形状。
        /// </summary>
        /// <param name="shape">形状。</param>
        /// <returns>形状结果。</returns>
        public object ShapeExecute(object shape)
        {
            if (shape == null)
            {
                return new HtmlString(string.Empty);
            }

            var context = new DisplayContext { Display = this, Value = shape, ViewContext = ViewContext, ViewDataContainer = ViewDataContainer };
            return _displayManager.Execute(context);
        }
        public IHtmlString Execute(DisplayContext context)
        {
            var shape = ConvertAsShapeCallsite.Target(ConvertAsShapeCallsite, context.Value);

            if (shape == null)
            {
                return(CoerceHtmlString(context.Value));
            }

            var shapeMetadata = shape.Metadata;

            if (shapeMetadata == null || string.IsNullOrEmpty(shapeMetadata.Type))
            {
                return(CoerceHtmlString(context.Value));
            }

            var workContext = _workContextAccessor.GetContext();
            var shapeTable  = _httpContextAccessor.Current() != null
                ? _shapeTableLocator.Value.Lookup(workContext.GetCurrentTheme().Id)
                : _shapeTableLocator.Value.Lookup(null);

            var displayingContext = new ShapeDisplayingContext
            {
                Shape         = shape,
                ShapeMetadata = shapeMetadata
            };

            _shapeDisplayEvents.Invoke(sde => sde.Displaying(displayingContext), Logger);

            ShapeBinding shapeBinding;

            if (TryGetDescriptorBinding(shapeMetadata.Type, Enumerable.Empty <string>(), shapeTable, out shapeBinding))
            {
                shapeBinding.ShapeDescriptor.Displaying.Invoke(action => action(displayingContext), Logger);

                shapeMetadata.BindingSources = shapeBinding.ShapeDescriptor.BindingSources.Where(x => x != null).ToList();
                if (!shapeMetadata.BindingSources.Any())
                {
                    shapeMetadata.BindingSources.Add(shapeBinding.ShapeDescriptor.BindingSource);
                }
            }

            shapeMetadata.Displaying.Invoke(action => action(displayingContext), Logger);

            if (displayingContext.ChildContent != null)
            {
                shape.Metadata.ChildContent = displayingContext.ChildContent;
            }
            else
            {
                ShapeBinding actualBinding;
                if (TryGetDescriptorBinding(shapeMetadata.Type, shapeMetadata.Alternates, shapeTable, out actualBinding))
                {
                    shape.Metadata.ChildContent = Process(actualBinding, shape, context);
                }
                else
                {
                    throw new RabbitException(T("找不到形状类型 {0}", shapeMetadata.Type));
                }
            }

            foreach (var frameType in shape.Metadata.Wrappers)
            {
                ShapeBinding frameBinding;
                if (TryGetDescriptorBinding(frameType, Enumerable.Empty <string>(), shapeTable, out frameBinding))
                {
                    shape.Metadata.ChildContent = Process(frameBinding, shape, context);
                }
            }

            var displayedContext = new ShapeDisplayedContext
            {
                Shape         = shape,
                ShapeMetadata = shape.Metadata,
                ChildContent  = shape.Metadata.ChildContent,
            };

            _shapeDisplayEvents.Invoke(sde =>
            {
                var prior = displayedContext.ChildContent = displayedContext.ShapeMetadata.ChildContent;
                sde.Displayed(displayedContext);
                if (prior != displayedContext.ChildContent)
                {
                    displayedContext.ShapeMetadata.ChildContent = displayedContext.ChildContent;
                }
            }, Logger);

            if (shapeBinding != null)
            {
                shapeBinding.ShapeDescriptor.Displayed.Invoke(action =>
                {
                    var prior = displayedContext.ChildContent = displayedContext.ShapeMetadata.ChildContent;
                    action(displayedContext);
                    if (prior != displayedContext.ChildContent)
                    {
                        displayedContext.ShapeMetadata.ChildContent = displayedContext.ChildContent;
                    }
                }, Logger);
            }

            shapeMetadata.Displayed.Invoke(action => action(displayedContext), Logger);

            return(shape.Metadata.ChildContent);
        }