当前位置: 首页>>代码示例>>C#>>正文


C# IResource.OpenForReading方法代码示例

本文整理汇总了C#中IResource.OpenForReading方法的典型用法代码示例。如果您正苦于以下问题:C# IResource.OpenForReading方法的具体用法?C# IResource.OpenForReading怎么用?C# IResource.OpenForReading使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IResource的用法示例。


在下文中一共展示了IResource.OpenForReading方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Parse

		/// <summary>
		/// Parses a script from the specified resource.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="resource">The resource wcich to parse as script.</param>
		/// <returns>Returns the parsed script.</returns>
		/// <exception cref="ParseScriptException">Thrown when an exception occurres while parsing the script.</exception>
		public IExpressionScript Parse(IMansionContext context, IResource resource)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (resource == null)
				throw new ArgumentNullException("resource");

			// open the script
			var cacheKey = ResourceCacheKey.Create(resource);
			return cachingService.GetOrAdd(
				context,
				cacheKey,
				() =>
				{
					// get the phrase
					string rawPhrase;
					using (var resourceStream = resource.OpenForReading())
					using (var reader = resourceStream.Reader)
						rawPhrase = reader.ReadToEnd();

					// let someone else do the heavy lifting
					var phrase = ParsePhrase(context, rawPhrase);

					// create the cache object
					return new CachedPhrase(phrase);
				});
		}
开发者ID:Erikvl87,项目名称:Premotion-Mansion,代码行数:35,代码来源:ExpressionScriptService.cs

示例2: Open

		/// <summary>
		/// Opens a template.
		/// </summary>
		/// <param name="context">The <see cref="IMansionContext"/>.</param>
		/// <param name="resource">The resource which to open.</param>
		/// <returns>Returns a marker which will close the template automatically.</returns>
		public IDisposable Open(IMansionContext context, IResource resource)
		{
			// validate arguments
			if (context == null)
				throw new ArgumentNullException("context");
			if (resource == null)
				throw new ArgumentNullException("resource");

			// open the template
			var cacheKey = ResourceCacheKey.Create(resource);
			var template = cachingService.GetOrAdd(
				context,
				cacheKey,
				() => {
					// read the complete template into memory
					string rawTemplate;
					using (var resourceStream = resource.OpenForReading())
					using (var reader = resourceStream.Reader)
						rawTemplate = reader.ReadToEnd();

					// loop through all the sections
					var sections = (from rawSection in sectionTokenizer.Tokenize(context, rawTemplate)
					                let interpreter = Election<SectionInterpreter, string>.Elect(context, interpreters, rawSection)
					                select interpreter.Interpret(context, rawSection)
					               ).ToList();

					// create the template
					var htmlTemplate = new Template(sections, resource.Path);

					// create the cached template
					return new CachedHtmlTemplate(htmlTemplate);
				});

			// push the template to the stack
			return context.TemplateStack.Push(template);
		}
开发者ID:Erikvl87,项目名称:Premotion-Mansion,代码行数:42,代码来源:HtmlTemplateService.cs


注:本文中的IResource.OpenForReading方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。