/// <summary>
        /// Parses the specified set of templates.
        /// </summary>
        /// <param name="razorTemplates">The set of string templates to parse.</param>
        /// <param name="models">
        /// The set of models or NULL if no models exist for all templates.
        /// Individual elements in this set may be NULL if no model exists for a specific template.
        /// </param>
        /// <param name="viewBags">
        /// The set of initial ViewBag contents or NULL for an initially empty ViewBag for all templates.
        /// Individual elements in this set may be NULL if an initially empty ViewBag is desired for a specific template.
        /// </param>
        /// <param name="cacheNames">
        /// The set of cache names or NULL if no caching is desired for all templates.
        /// Individual elements in this set may be NULL if caching is not desired for specific template.
        /// </param>
        /// <param name="parallel">Flag to determine whether parsing in templates.</param>
        /// <returns>The set of parsed template results.</returns>
        public IEnumerable <string> ParseMany(IEnumerable <string> razorTemplates, IEnumerable <object> models, IEnumerable <DynamicViewBag> viewBags, IEnumerable <string> cacheNames, bool parallel)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if ((models != null) && (models.Count() != razorTemplates.Count()))
            {
                throw new ArgumentException("Expected same number of models contents as templates to be processed.");
            }

            if ((viewBags != null) && (viewBags.Count() != razorTemplates.Count()))
            {
                throw new ArgumentException("Expected same number of ViewBag contents as templates to be processed.");
            }

            if ((cacheNames != null) && (cacheNames.Count() != razorTemplates.Count()))
            {
                throw new ArgumentException("Expected same number of cache names as templates to be processed.");
            }

            if (models != null)
            {
                foreach (object model in models)
                {
                    if ((model != null) && CompilerServicesUtility.IsDynamicType(model.GetType()))
                    {
                        throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
                    }
                }
            }

            return(_proxy.ParseMany(razorTemplates, models, viewBags, cacheNames, parallel).ToList());
        }
Exemple #2
0
 /// <summary>
 /// Backwards Compat
 /// </summary>
 /// <param name="razorTemplate"></param>
 /// <param name="model"></param>
 /// <param name="viewBag"></param>
 /// <param name="cacheName"></param>
 /// <returns></returns>
 public string Parse(string razorTemplate, object model, DynamicViewBag viewBag, string cacheName)
 {
     CheckDisposed();
     if (model != null &&
         (CompilerServicesUtility.IsAnonymousType(model.GetType()) || CompilerServicesUtility.IsDynamicType(model.GetType())))
     {
         throw new ArgumentException("Anonymous types are not supported (use the new RazorEngineService/IsolatedRazorEngineService API)");
     }
     return(_proxy.Parse(razorTemplate, model, viewBag, cacheName));
 }
        /// <summary>
        /// Creates a <see cref="Type"/> that can be used to instantiate an instance of a template.
        /// </summary>
        /// <param name="razorTemplate">The string template.</param>
        /// <param name="modelType">The model type or NULL if no model exists.</param>
        /// <returns>An instance of <see cref="Type"/>.</returns>
        public Type CreateTemplateType(string razorTemplate, Type modelType)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if (CompilerServicesUtility.IsDynamicType(modelType))
            {
                throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
            }

            return(_proxy.CreateTemplateType(razorTemplate, modelType));
        }
        /// <summary>
        /// Parses the specified array of templates.
        /// </summary>
        /// <typeparam name="T">The model type.</typeparam>
        /// <param name="razorTemplates">The array of string templates to partse.</param>
        /// <param name="models">The array of models.</param>
        /// <param name="names">The array of cache names.</param>
        /// <param name="parallel">Flag to determine whether parsing in templates.</param>
        /// <returns>The array of parsed template results.</returns>
        public string[] ParseMany <T>(string[] razorTemplates, T[] models, string[] names, bool parallel = false)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if (CompilerServicesUtility.IsDynamicType(typeof(T)))
            {
                throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
            }

            return(_proxy.ParseMany(razorTemplates, models, names, parallel).ToArray());
        }
        /// <summary>
        /// Parses and returns the result of the specified string template.
        /// </summary>
        /// <param name="razorTemplate">The string template.</param>
        /// <param name="model">The model instance.</param>
        /// <param name="name">The name of the template type in the cache.</param>
        /// <returns>The string result of the template.</returns>
        public string Parse(string razorTemplate, object model, string name)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if (CompilerServicesUtility.IsDynamicType(model.GetType()))
            {
                throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
            }

            return(_proxy.Parse(razorTemplate, model, name));
        }
        /// <summary>
        /// Gets the set of template instances for the specified string templates. Cached templates will be considered
        /// and if they do not exist, new types will be created and instantiated.
        /// </summary>
        /// <typeparam name="T">The model type.</typeparam>
        /// <param name="razorTemplates">The set of templates to create.</param>
        /// <param name="models">The set of models.</param>
        /// <param name="names">The set of cache names.</param>
        /// <param name="parallel">Flag to determine whether to get the templates in parallel.</param>
        /// <returns>The set of <see cref="ITemplate"/> instances.</returns>
        public IEnumerable <ITemplate> GetTemplates <T>(IEnumerable <string> razorTemplates, IEnumerable <T> models, IEnumerable <string> names, bool parallel = false)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if (CompilerServicesUtility.IsDynamicType(typeof(T)))
            {
                throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
            }

            return(_proxy.GetTemplates(razorTemplates, models, names, parallel));
        }
        /// <summary>
        /// Creates a set of template types from the specfied string templates.
        /// </summary>
        /// <param name="razorTemplates">The set of templates to create <see cref="Type"/> instances for.</param>
        /// <param name="modelType">The model type.</param>
        /// <param name="parallel">Flag to determine whether to create template types in parallel.</param>
        /// <returns>The set of <see cref="Type"/> instances.</returns>
        public IEnumerable <Type> CreateTemplateTypes(IEnumerable <string> razorTemplates, Type modelType, bool parallel = false)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if (modelType != null && CompilerServicesUtility.IsDynamicType(modelType))
            {
                throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
            }

            return(_proxy.CreateTemplateTypes(razorTemplates, modelType, parallel));
        }
Exemple #8
0
        /// <summary>
        ///     Creates an instance of <see cref="ITemplate{T}" /> from the specified string template.
        /// </summary>
        /// <param name="razorTemplate">
        ///     The string template.
        ///     If templateType is not NULL, this parameter may be NULL (unused).
        /// </param>
        /// <param name="templateType">
        ///     The template type or NULL if the template type should be dynamically created.
        ///     If razorTemplate is not NULL, this parameter may be NULL (unused).
        /// </param>
        /// <param name="model">The model instance or NULL if no model exists.</param>
        /// <param name="logger"></param>
        /// <returns>
        ///     An instance of <see cref="ITemplate{T}" />.
        /// </returns>
        public ITemplate CreateTemplate(string razorTemplate, Type templateType, object model, ILogger logger)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if (model != null)
            {
                if (CompilerServicesUtility.IsDynamicType(model.GetType()))
                {
                    throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
                }
            }

            return(_proxy.CreateTemplate(razorTemplate, templateType, model, logger));
        }
Exemple #9
0
 private Type CheckModelType(Type modelType)
 {
     if (modelType == null)
     {
         return(null);
     }
     if (CompilerServicesUtility.IsAnonymousTypeRecursive(modelType))
     {
         //throw new ArgumentException("Cannot use anonymous type as model type.");
         modelType = null;
     }
     if (modelType != null && CompilerServicesUtility.IsDynamicType(modelType))
     {
         modelType = null;
     }
     return(modelType);
 }
        public void ParseToWriter <T>(System.IO.TextWriter writer, string razorTemplate, object model, DynamicViewBag viewBag, string cacheName)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if (model != null)
            {
                if (CompilerServicesUtility.IsDynamicType(model.GetType()))
                {
                    throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
                }
            }

            _proxy.ParseToWriter <T>(writer, razorTemplate, model, viewBag, cacheName);
        }
Exemple #11
0
        private Tuple <object, Type> CheckModel(object model)
        {
            if (model == null)
            {
                return(Tuple.Create((object)null, (Type)null));
            }
            Type modelType = (model == null) ? typeof(object) : model.GetType();

            bool isAnon = CompilerServicesUtility.IsAnonymousTypeRecursive(modelType);

            if (isAnon ||
                CompilerServicesUtility.IsDynamicType(modelType))
            {
                modelType = null;
                if (isAnon || Configuration.AllowMissingPropertiesOnDynamic)
                {
                    model = RazorDynamicObject.Create(model, Configuration.AllowMissingPropertiesOnDynamic);
                }
            }
            return(Tuple.Create(model, modelType));
        }
        /// <summary>
        /// Gets the set of template instances for the specified string templates. Cached templates will be considered
        /// and if they do not exist, new types will be created and instantiated.
        /// </summary>
        /// <param name="razorTemplates">The set of templates to create.</param>
        /// <param name="models">
        /// The set of models or NULL if no models exist for all templates.
        /// Individual elements in this set may be NULL if no model exists for a specific template.
        /// </param>
        /// <param name="cacheNames">The set of cache names.</param>
        /// <param name="parallel">Flag to determine whether to get the templates in parallel.</param>
        /// <returns>The set of <see cref="ITemplate"/> instances.</returns>
        public IEnumerable <ITemplate> GetTemplates(IEnumerable <string> razorTemplates, IEnumerable <object> models, IEnumerable <string> cacheNames, bool parallel = false)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

            if (models != null)
            {
                foreach (object model in models)
                {
                    if (model != null)
                    {
                        if (CompilerServicesUtility.IsDynamicType(model.GetType()))
                        {
                            throw new ArgumentException("IsolatedTemplateService instances do not support anonymous or dynamic types.");
                        }
                    }
                }
            }

            return(_proxy.GetTemplates(razorTemplates, models, cacheNames, parallel));
        }
Exemple #13
0
        /// <summary>
        /// Builds a type name for the specified generic type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="isDynamic">Specifies whether the type is dynamic.</param>
        /// <returns>
        /// The string typename (including namespace and generic type parameters).
        /// </returns>
        public override string BuildTypeNameInternal(Type type, bool isDynamic)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (!type.IsGenericType)
            {
                return(type.FullName);
            }

            return(type.Namespace
                   + "."
                   + type.Name.Substring(0, type.Name.IndexOf('`'))
                   + "(Of "
                   + (isDynamic ? "Object" : string.Join(", ", type.GetGenericArguments().Select(t => BuildTypeNameInternal(t, CompilerServicesUtility.IsDynamicType(t)))))
                   + ")");
        }