コード例 #1
0
        /// <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 or NULL if no model exists.</param>
        /// <param name="viewBag">The ViewBag contents or NULL for an initially empty ViewBag.</param>
        /// <param name="cacheName">The name of the template type in the cache or NULL if no caching is desired.</param>
        /// <returns>The string result of the template.</returns>
        public string Parse <T>(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.");
                }
            }

            return(_proxy.Parse <T>(razorTemplate, model, viewBag, cacheName));
        }
コード例 #2
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>
        /// <returns>An instance of <see cref="ITemplate{T}"/>.</returns>
        public ITemplate CreateTemplate(string razorTemplate, Type templateType, object model)
        {
            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));
        }
コード例 #3
0
        public IEnumerable <CompilerReference> GetReferences(TypeContext context, IEnumerable <CompilerReference> includeAssemblies = null)
        {
            var allAssembly = CompilerServicesUtility
                              .GetLoadedAssemblies();

            var AssemblyToReference = allAssembly
                                      .Where(a => !a.IsDynamic && File.Exists(a.Location) && !a.Location.Contains("CompiledRazorTemplates.Dynamic") && a.FullName.IndexOf("System.Web.Helpers") < 0) //(CompilerServiceBase.DynamicTemplateNamespace))
                                      .GroupBy(a => a.GetName().Name).Select(grp => grp.First(y => y.GetName().Version == grp.Max(x => x.GetName().Version)))                                        // only select distinct assemblies based on FullName to avoid loading duplicate assemblies
                                      .Select(a => CompilerReference.From(a))
                                      .Concat(includeAssemblies ?? Enumerable.Empty <CompilerReference>());

            yield return(CompilerReference.From(HostingEnvironment.ApplicationPhysicalPath + @"Modules\Laser.Orchard.StartupConfig\bin\System.Web.Helpers.dll"));

            foreach (var assembly in AssemblyToReference)
            {
                yield return(assembly);
            }
        }
コード例 #4
0
        /// <summary>
        /// Checks if we need to wrap the given model in
        /// an <see cref="RazorDynamicObject"/> instance and wraps it.
        /// </summary>
        /// <param name="modelType">the model-type</param>
        /// <param name="original">the original model</param>
        /// <param name="allowMissing">true when we should allow missing properties on dynamic models.</param>
        /// <returns>the original model or an wrapper object.</returns>
        internal static object GetDynamicModel(Type modelType, object original, bool allowMissing)
        {
            object result = original;

            if (modelType == null && original != null)
            {
                // We try to make some things work:
                if (CompilerServicesUtility.IsAnonymousTypeRecursive(original.GetType()))
                {
                    // TODO: we should handle Configuration.AllowMissingPropertiesOnDynamic
                    result = RazorDynamicObject.Create(original, allowMissing);
                }
                else if (allowMissing)
                {
                    result = RazorDynamicObject.Create(original, allowMissing);
                }
            }
            return(result);
        }
コード例 #5
0
 /// <summary>
 /// See <see cref="IReferenceResolver.GetReferences(TypeContext,IEnumerable{CompilerReference})" />
 /// </summary>
 /// <param name="context"></param>
 /// <param name="includeAssemblies"></param>
 /// <returns></returns>
 public IEnumerable <CompilerReference> GetReferences(
     TypeContext context = null,
     IEnumerable <CompilerReference> includeAssemblies = null)
 {
     return(CompilerServicesUtility.GetLoadedAssemblies()
            .Where(
                a =>
     {
         if (!a.IsDynamic && File.Exists(a.Location))
         {
             return !a.Location.Contains("CompiledRazorTemplates.Dynamic");
         }
         return false;
     })
            .GroupBy(a => a.GetName().Name)
            .Select(grp => grp.First(y => y.GetName().Version == grp.Max(x => x.GetName().Version)))
            .Select(CompilerReference.From)
            .Concat(includeAssemblies ?? Enumerable.Empty <CompilerReference>()));
 }
コード例 #6
0
        /// <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="modelTypes">
        /// The set of model types 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="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, IEnumerable <Type> modelTypes, bool parallel = false)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("IsolatedTemplateService");
            }

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

            return(_proxy.CreateTemplateTypes(razorTemplates, modelTypes, parallel));
        }
コード例 #7
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));
        }
コード例 #8
0
        static void LoadMetadataReference()
        {
            Tools.MapPath("");
            Tools.GetId();
            string replstr    = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "file:///" : "file://";
            var    assemblies = CompilerServicesUtility
                                .GetLoadedAssemblies()
                                .Where(a => !a.IsDynamic && File.Exists(a.CodeBase.Replace(replstr, "")))
                                .Select(a => (a.CodeBase.Replace(replstr, "")));

            int c = assemblies.Count();

            Constant.BaseNamespaces = new MetadataReference[c];

            int i = 0;

            foreach (string item in assemblies)
            {
                Constant.BaseNamespaces[i] = (MetadataReference.CreateFromFile(item));
                i++;
            }
        }
コード例 #9
0
        /// <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));
        }
コード例 #10
0
 private SpanCodeGenerator CreateModelCodeGenerator(string model)
 {
     return(new SetModelTypeCodeGenerator(model, (templateType, modelTypeName) => {
         return CompilerServicesUtility.CSharpCreateGenericType(templateType, modelTypeName, true);
     }));
 }
コード例 #11
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)))))
                   + ")");
        }
コード例 #12
0
        public void CompilerServiceUtility_CheckNormalGenericClass()
        {
            var result = CompilerServicesUtility.CSharpGetRawTypeName(typeof(AddLanguageInfo_Viewbag <>));

            Assert.AreEqual("Test.RazorEngine.TestTypes.BaseTypes.AddLanguageInfo_Viewbag", result);
        }
コード例 #13
0
        public void CompilerServiceUtility_CheckNestedClass()
        {
            var result = CompilerServicesUtility.CSharpGetRawTypeName(typeof(HostingClass.NestedBaseClass <>));

            Assert.AreEqual("Test.RazorEngine.TestTypes.BaseTypes.HostingClass.NestedBaseClass", result);
        }
コード例 #14
0
        public void CompilerServiceUtility_CheckNonGenericNestedType()
        {
            var result = CompilerServicesUtility.ResolveCSharpTypeName(typeof(HostingClass.NestedClass));

            Assert.AreEqual("Test.RazorEngine.TestTypes.BaseTypes.HostingClass.NestedClass", result);
        }
コード例 #15
0
        public void CompilerServiceUtility_CheckNonGenericType()
        {
            var result = CompilerServicesUtility.ResolveCSharpTypeName(typeof(System.String));

            Assert.AreEqual("System.String", result);
        }
コード例 #16
0
        public void CompilerServiceUtility_CheckDynamicType()
        {
            var result = CompilerServicesUtility.ResolveCSharpTypeName(typeof(System.Dynamic.DynamicObject));

            Assert.AreEqual("dynamic", result);
        }