Example #1
0
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 public RazorControl()
     : base()
 {
     // look up template
     template = Razor.Template(this, GetType());
     if (template == null)
     {
         throw new RazorException("Could not load Razor template for {0}.", this);
     }
 }
Example #2
0
        /// <summary>
        /// Renders the specified template to the writer.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="writer"></param>
        /// <param name="template"></param>
        public static void Render <T>(HtmlTextWriter writer, IRazorControlTemplate <T> template)
            where T : CogitoControl
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            template.Render(writer);
        }
Example #3
0
        /// <summary>
        /// Renders the Razor template configured for the specified <see cref="CogitoControl"/> instance, to the specified
        /// <see cref="HtmlTextWriter"/>. This method is best invoked from the <see cref="CogitoControl"/>.Render method.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="control"></param>
        public static void Render(HtmlTextWriter writer, IRazorControlTemplate template)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }
            if (template.Control == null)
            {
                throw new ArgumentNullException(nameof(template));
            }
            if (template.ControlType == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            RenderMethod.MakeGenericMethod(template.ControlType)
            .Invoke(null, new object[] { writer, template });
        }
Example #4
0
        /// <summary>
        /// Loads and returns the template for the specified <see cref="CogitoControl"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="control"></param>
        /// <returns></returns>
        public static IRazorControlTemplate Template <T>(T control)
            where T : Control
        {
            if (control == null)
            {
                throw new ArgumentNullException(nameof(control));
            }

            lock (syncRoot)
            {
                // recursively inherit all references of the control being referenced and ourself
                var referencedAssemblies = Enumerable.Empty <Assembly>()
                                           .Append(typeof(Razor).Assembly)
                                           .Append(typeof(CogitoControl).Assembly)
                                           .Append(typeof(T).Assembly);

                referencedAssemblies = referencedAssemblies
                                       .SelectMany(i => i.GetReferencedAssemblies())
                                       .Select(i => Assembly.Load(i))
                                       .Concat(referencedAssemblies);

                var referencedAssemblyPaths = referencedAssemblies
                                              .Select(i => new Uri(i.Location))
                                              .Select(i => i.LocalPath);

                // obtain or create the Razor template for the given control
                var type = RazorTemplateBuilder.GetOrBuildType(
                    FindTemplate(typeof(T)),
                    defaultBaseClass: typeof(RazorControlTemplate <T>),
                    referencedAssemblies: referencedAssemblyPaths,
                    importedNamespaces: new[] { typeof(CogitoControl).Namespace },
                    innerTemplateType: typeof(HtmlHelperResult),
                    cacheKey: typeof(T).FullName);
                if (type == null)
                {
                    throw new NullReferenceException("Unable to locate newly generated Type.");
                }

                IRazorControlTemplate template = null;

                // generic constructor
                var ctor1 = type.GetConstructor(new[] { typeof(T) });
                if (ctor1 != null)
                {
                    template = (IRazorControlTemplate)ctor1.Invoke(new[] { control });
                }

                // non generic constructor
                var ctor2 = type.GetConstructor(new[] { typeof(CogitoControl) });
                if (ctor2 != null)
                {
                    template = (IRazorControlTemplate)ctor2.Invoke(new[] { control });
                }

                if (template == null)
                {
                    throw new NullReferenceException("Could not construct new template instance. Could find compatible constructor.");
                }

                return(template);
            }
        }