Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        public void Test_simple_helper_code_generation()
        {
            var t = RazorTemplateBuilder.ToCode(LoadTemplateText("SimpleWithHelper.cshtml").ReadToEnd());

            Assert.IsTrue(t.Contains(@"@__CompiledTemplate"));
        }