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


C# AssemblyBuilder.BuildAssembly方法代码示例

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


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

示例1: Build


//.........这里部分代码省略.........
					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 (
				AppDomain.CurrentDomain.SetupInformation.DynamicBase,
				name, "dll", OnCreateTemporaryAssemblyFile);
			parameters.OutputAssembly = outputAssemblyName;
			foreach (Assembly a in BuildManager.TopLevelAssemblies)
				parameters.ReferencedAssemblies.Add (a.Location);
			CompilerResults results = abuilder.BuildAssembly (parameters);
			if (results == null)
				return;
			
			if (results.NativeCompilerReturnValue == 0) {
				BuildManager.CodeAssemblies.Add (results.CompiledAssembly);
				BuildManager.TopLevelAssemblies.Add (results.CompiledAssembly);
				HttpRuntime.WritePreservationFile (results.CompiledAssembly, name);
			} else {
				if (HttpContext.Current.IsCustomErrorEnabled)
					throw new HttpException ("An error occurred while initializing application.");
				throw new CompilationException (null, results.Errors, null);
			}
		}
开发者ID:stabbylambda,项目名称:mono,代码行数:101,代码来源:AppCodeCompiler.cs

示例2: BuildDefaultAssembly

		void BuildDefaultAssembly (List <string> files, CodeCompileUnit unit)
		{
			AssemblyBuilder abuilder = new AssemblyBuilder (Provider);
			if (unit != null)
				abuilder.AddCodeCompileUnit (unit);
			
			CompilerParameters cp = ci.CreateDefaultCompilerParameters ();
			cp.OutputAssembly = baseAssemblyPath;
			cp.GenerateExecutable = false;
			cp.TreatWarningsAsErrors = true;
			cp.IncludeDebugInformation = config.Debug;

			foreach (string f in files)
				cp.EmbeddedResources.Add (f);
			
			CompilerResults results = abuilder.BuildAssembly (cp);
			if (results == null)
				return;
			
			if (results.NativeCompilerReturnValue == 0) {
				mainAssembly = results.CompiledAssembly;
				BuildManager.TopLevelAssemblies.Add (mainAssembly);
			} else {
				if (HttpContext.Current.IsCustomErrorEnabled)
					throw new ApplicationException ("An error occurred while compiling global resources.");
				throw new CompilationException (null, results.Errors, null);
			}
			
			HttpRuntime.WritePreservationFile (mainAssembly, canonicAssemblyName);
			HttpRuntime.EnableAssemblyMapping (true);
		}
开发者ID:nlhepler,项目名称:mono,代码行数:31,代码来源:AppResourcesAssemblyBuilder.cs

示例3: 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

示例4: GenerateAssembly

		static void GenerateAssembly (AssemblyBuilder abuilder, BuildProviderGroup group, VirtualPath vp, bool debug)
		{
			IDictionary <string, bool> deps;
			BuildManagerCacheItem bmci;
			string bvp, vpabsolute = vp.Absolute;
			StringBuilder sb;
			string newline;
			int failedCount = 0;
			
			if (debug) {
				newline = Environment.NewLine;
				sb = new StringBuilder ("Code generation for certain virtual paths in a batch failed. Those files have been removed from the batch." + newline);
				sb.Append ("Since you're running in debug mode, here's some more information about the error:" + newline);
			} else {
				newline = null;
				sb = null;
			}
			
			List <BuildProvider> failedBuildProviders = null;
			StringComparison stringComparison = RuntimeHelpers.StringComparison;
			foreach (BuildProvider bp in group) {
				bvp = bp.VirtualPath;
				if (HasCachedItemNoLock (bvp))
					continue;
				
				try {
					bp.GenerateCode (abuilder);
				} catch (Exception ex) {
					if (String.Compare (bvp, vpabsolute, stringComparison) == 0) {
						if (ex is CompilationException || ex is ParseException)
							throw;
						
						throw new HttpException ("Code generation failed.", ex);
					}
					
					if (failedBuildProviders == null)
						failedBuildProviders = new List <BuildProvider> ();
					failedBuildProviders.Add (bp);
					failedCount++;
					if (sb != null) {
						if (failedCount > 1)
							sb.Append (newline);
						
						sb.AppendFormat ("Failed file virtual path: {0}; Exception: {1}{2}{1}", bp.VirtualPath, newline, ex);
					}
					continue;
				}
				
				deps = bp.ExtractDependencies ();
				if (deps != null) {
					foreach (var dep in deps) {
						bmci = GetCachedItemNoLock (dep.Key);
						if (bmci == null || bmci.BuiltAssembly == null)
							continue;
						abuilder.AddAssemblyReference (bmci.BuiltAssembly);
					}
				}
			}

			if (sb != null && failedCount > 0)
				ShowDebugModeMessage (sb.ToString ());
			
			if (failedBuildProviders != null) {
				foreach (BuildProvider bp in failedBuildProviders)
					group.Remove (bp);
			}
			
			foreach (Assembly asm in referencedAssemblies) {
				if (asm == null)
					continue;
				
				abuilder.AddAssemblyReference (asm);
			}
			
			CompilerResults results  = abuilder.BuildAssembly (vp);
			
			// No results is not an error - it is possible that the assembly builder contained only .asmx and
			// .ashx files which had no body, just the directive. In such case, no code unit or code file is added
			// to the assembly builder and, in effect, no assembly is produced but there are STILL types that need
			// to be added to the cache.
			Assembly compiledAssembly = results != null ? results.CompiledAssembly : null;
			try {
				buildCacheLock.EnterWriteLock ();
				if (compiledAssembly != null)
					referencedAssemblies.Add (compiledAssembly);
				
				foreach (BuildProvider bp in group) {
					if (HasCachedItemNoLock (bp.VirtualPath))
						continue;
					
					StoreInCache (bp, compiledAssembly, results);
				}
			} finally {
				buildCacheLock.ExitWriteLock ();
			}
		}
开发者ID:Profit0004,项目名称:mono,代码行数:96,代码来源:BuildManager.cs

示例5: Compile

		public void Compile ()
		{
			string refsPath = Path.Combine (HttpRuntime.AppDomainAppPath, ResourcesDirName);
			if (!Directory.Exists (refsPath))
				return;

			string[] files = Directory.GetFiles (refsPath, "*.wsdl", SearchOption.AllDirectories);
			if (files == null || files.Length == 0)
				return;

			CompilationSection cs = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
			if (cs == null)
				throw new HttpException ("Unable to determine default compilation language.");

			CompilerType ct = BuildManager.GetDefaultCompilerTypeForLanguage (cs.DefaultLanguage, cs);
			CodeDomProvider codeDomProvider = null;
			Exception codeDomException = null;
			
			try {
				codeDomProvider = Activator.CreateInstance (ct.CodeDomProviderType) as CodeDomProvider;
			} catch (Exception e) {
				codeDomException = e;
			}

			if (codeDomProvider == null)
				throw new HttpException ("Unable to instantiate default compilation language provider.", codeDomException);

			AssemblyBuilder ab = new AssemblyBuilder (codeDomProvider, "App_WebReferences_");
			ab.CompilerOptions = ct.CompilerParameters;
			
			VirtualPath vp;
			WsdlBuildProvider wbp;
			foreach (string file in files) {
				vp = VirtualPath.PhysicalToVirtual (file);
				if (vp == null)
					continue;
				
				wbp = new WsdlBuildProvider ();
				wbp.SetVirtualPath (vp);
				wbp.GenerateCode (ab);
			}

			CompilerResults results;
			try {
				results = ab.BuildAssembly ();
			} catch (CompilationException ex) {
				throw new HttpException ("Failed to compile web references.", ex);
			}

			if (results == null)
				return;
			
			Assembly asm = results.CompiledAssembly;
			BuildManager.TopLevelAssemblies.Add (asm);
		}
开发者ID:nlhepler,项目名称:mono,代码行数:55,代码来源:AppWebReferencesCompiler.cs


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