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


C# AssemblyBuilder.AddCodeFile方法代码示例

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


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

示例1: Build

		// Build and add the assembly to the BuildManager's
		// CodeAssemblies collection
		public void Build (string[] binAssemblies)
		{
			Type compilerProvider = null;
			CompilerInfo compilerInfo = null, cit;
			string extension, language, cpfile = null;
			List<string> knownfiles = new List<string>();
			List<string> unknownfiles = new List<string>();
			
			// First make sure all the files are in the same
			// language
			bool known = false;
			foreach (string f in files) {
				known = true;
				language = null;
				
				extension = Path.GetExtension (f);
				if (String.IsNullOrEmpty (extension) || !CodeDomProvider.IsDefinedExtension (extension))
					known = false;
				if (known) {
					language = CodeDomProvider.GetLanguageFromExtension(extension);
					if (!CodeDomProvider.IsDefinedLanguage (language))
						known = false;
				}
				if (!known || language == null) {
					unknownfiles.Add (f);
					continue;
				}
				
				cit = CodeDomProvider.GetCompilerInfo (language);
				if (cit == null || !cit.IsCodeDomProviderTypeValid)
					continue;
				if (compilerProvider == null) {
					cpfile = f;
					compilerProvider = cit.CodeDomProviderType;
					compilerInfo = cit;
				} else if (compilerProvider != cit.CodeDomProviderType)
					throw new HttpException (
						String.Format (
							"Files {0} and {1} are in different languages - they cannot be compiled into the same assembly",
							Path.GetFileName (cpfile),
							Path.GetFileName (f)));
				knownfiles.Add (f);
			}

			CodeDomProvider provider = null;
			CompilationSection compilationSection = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
			if (compilerInfo == null) {
				if (!CodeDomProvider.IsDefinedLanguage (compilationSection.DefaultLanguage))
					throw new HttpException ("Failed to retrieve default source language");
				compilerInfo = CodeDomProvider.GetCompilerInfo (compilationSection.DefaultLanguage);
				if (compilerInfo == null || !compilerInfo.IsCodeDomProviderTypeValid)
					throw new HttpException ("Internal error while initializing application");
			}

			provider = compilerInfo.CreateProvider ();
			if (provider == null)
				throw new HttpException ("A code provider error occurred while initializing application.");

			AssemblyBuilder abuilder = new AssemblyBuilder (provider);
			foreach (string file in knownfiles)
				abuilder.AddCodeFile (file);
			foreach (CodeCompileUnit unit in units)
				abuilder.AddCodeCompileUnit (unit);
			
			BuildProvider bprovider;
			CompilerParameters parameters = compilerInfo.CreateDefaultCompilerParameters ();
			parameters.IncludeDebugInformation = compilationSection.Debug;
			
			if (binAssemblies != null && binAssemblies.Length > 0)
				parameters.ReferencedAssemblies.AddRange (binAssemblies);
			
			if (compilationSection != null) {
				foreach (AssemblyInfo ai in compilationSection.Assemblies)
					if (ai.Assembly != "*") {
						try {
							parameters.ReferencedAssemblies.Add (
								AssemblyPathResolver.GetAssemblyPath (ai.Assembly));
						} catch (Exception ex) {
							throw new HttpException (
								String.Format ("Could not find assembly {0}.", ai.Assembly),
								ex);
						}
					}
				
				BuildProviderCollection buildProviders = compilationSection.BuildProviders;
				
				foreach (string file in unknownfiles) {
					bprovider = GetBuildProviderFor (file, buildProviders);
					if (bprovider == null)
						continue;
					bprovider.GenerateCode (abuilder);
				}
			}

			if (knownfiles.Count == 0 && unknownfiles.Count == 0 && units.Count == 0)
				return;
			
			outputAssemblyName = (string)FileUtils.CreateTemporaryFile (
//.........这里部分代码省略.........
开发者ID:stabbylambda,项目名称:mono,代码行数:101,代码来源:AppCodeCompiler.cs

示例2: GetAssemblyFromSource

		Assembly GetAssemblyFromSource (string vpath)
		{			
			vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
			string realPath = MapPath (vpath, false);
			if (!File.Exists (realPath))
				ThrowParseException ("File " + vpath + " not found");

			AddSourceDependency (vpath);
			
			CompilerResults result;
			string tmp;
			CompilerParameters parameters;
			CodeDomProvider provider = BaseCompiler.CreateProvider (HttpContext.Current, language, out parameters, out tmp);
			if (provider == null)
				throw new HttpException ("Cannot find provider for language '" + language + "'.");
			
			AssemblyBuilder abuilder = new AssemblyBuilder (provider);
			abuilder.CompilerOptions = parameters;
			abuilder.AddAssemblyReference (BuildManager.GetReferencedAssemblies () as List <Assembly>);
			abuilder.AddCodeFile (realPath);
			result = abuilder.BuildAssembly (new VirtualPath (vpath));

			if (result.NativeCompilerReturnValue != 0) {
				using (StreamReader reader = new StreamReader (realPath)) {
					throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
				}
			}

			AddAssembly (result.CompiledAssembly, true);
			return result.CompiledAssembly;
		}		
开发者ID:Profit0004,项目名称:mono,代码行数:31,代码来源:TemplateParser.cs


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