Exemple #1
0
        /// <summary>
        /// 编译
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool Compile(Infrastructure.TemplateBase template)
        {
            if (template == null)
            {
                return(false);
            }
            if (string.IsNullOrEmpty(template.TemplateContent))
            {
                return(false);
            }

            InitRazorEngine();
            Engine.Razor.Compile(template.TemplateContent, template.Name, template.ViewModelType);
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// 加载
        /// </summary>
        /// <returns></returns>
        public bool Initialize()
        {
            string basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates");

            if (!Directory.Exists(basePath))
            {
                throw new DirectoryNotFoundException("template base dir");
            }

            DirectoryInfo dir = new DirectoryInfo(basePath);

            FileInfo[] files = dir.GetFiles("*.dll", SearchOption.AllDirectories);
            if (files == null || files.Length <= 0)
            {
                return(false);
            }

            Templates = new List <Infrastructure.TemplateBase>();
            foreach (var file in files)
            {
                try
                {
                    //加载DLL,每个DLL里查找ITemplate
                    Assembly asm       = Assembly.LoadFrom(file.FullName);
                    Type[]   templates = asm.GetTypes().Where(t => t.BaseType == typeof(Infrastructure.TemplateBase)).ToArray();

                    if (templates == null)
                    {
                        continue;
                    }
                    for (int i = 0; i < templates.Length; i++)
                    {
                        //找到后实例化
                        Infrastructure.TemplateBase template = Activator.CreateInstance(templates[i]) as Infrastructure.TemplateBase;
                        if (template == null)
                        {
                            continue;
                        }

                        //编译模板
                        if (!Compile(template))
                        {
                            continue;
                        }


                        //添加到模板列表
                        Templates.Add(template);
                    }
                }
                catch (Exception e)
                {
                    if (e is TemplateCompilationException)
                    {
                        throw e;
                    }
                    continue;
                }
            }

            return(true);
        }