1 //------------------------------------------------------------------------------
2 // <copyright file="XsdBuildProvider.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Web.Compilation {
12 using System.Data.Design;
13 using System.Diagnostics.CodeAnalysis;
14 using System.Globalization;
16 using System.Xml.Schema;
17 using System.Reflection;
19 using System.CodeDom.Compiler;
20 using System.Web.Hosting;
21 using System.Web.Configuration;
22 using System.Collections;
24 using Util=System.Web.UI.Util;
25 #if !FEATURE_PAL // FEATURE_PAL does not support System.Data.Design
26 using TypedDataSetGenerator=System.Data.Design.TypedDataSetGenerator;
27 #endif // !FEATURE_PAL
29 [BuildProviderAppliesTo(BuildProviderAppliesTo.Code)]
30 internal class XsdBuildProvider: BuildProvider {
32 [SuppressMessage("Microsoft.Security", "MSEC1207:UseXmlReaderForLoad", Justification = "Developer-controlled .xsd files in application directory are implicitly trusted by ASP.Net.")]
33 public override void GenerateCode(AssemblyBuilder assemblyBuilder) {
34 #if !FEATURE_PAL // FEATURE_PAL does not support System.Data.Design
35 // Get the namespace that we will use
36 string ns = Util.GetNamespaceFromVirtualPath(VirtualPathObject);
38 // We need to use XmlDocument to parse the xsd file is order to open it with the
39 // correct encoding (VSWhidbey 566286)
40 XmlDocument doc = new XmlDocument();
41 using (Stream stream = OpenStream()) {
44 String content = doc.OuterXml;
46 // Generate a CodeCompileUnit from the dataset
47 CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
49 CodeNamespace codeNamespace = new CodeNamespace(ns);
50 codeCompileUnit.Namespaces.Add(codeNamespace);
52 // Devdiv 18365, Dev10
54 bool isVer35OrAbove = CompilationUtil.IsCompilerVersion35OrAbove(assemblyBuilder.CodeDomProvider.GetType());
57 TypedDataSetGenerator.GenerateOption generateOptions = TypedDataSetGenerator.GenerateOption.None;
58 generateOptions |= TypedDataSetGenerator.GenerateOption.HierarchicalUpdate;
59 generateOptions |= TypedDataSetGenerator.GenerateOption.LinqOverTypedDatasets;
60 Hashtable customDBProviders = null;
61 TypedDataSetGenerator.Generate(content, codeCompileUnit, codeNamespace, assemblyBuilder.CodeDomProvider, customDBProviders, generateOptions);
64 TypedDataSetGenerator.Generate(content, codeCompileUnit, codeNamespace, assemblyBuilder.CodeDomProvider);
67 // Add all the assembly references needed by the generated code
68 if (TypedDataSetGenerator.ReferencedAssemblies != null) {
69 var isVer35 = CompilationUtil.IsCompilerVersion35(assemblyBuilder.CodeDomProvider.GetType());
70 foreach (Assembly a in TypedDataSetGenerator.ReferencedAssemblies) {
73 var aName = a.GetName();
74 if (aName.Name == "System.Data.DataSetExtensions") {
77 aName.Version = new Version(3, 5, 0, 0);
78 CompilationSection.RecordAssembly(aName.FullName, a);
81 assemblyBuilder.AddAssemblyReference(a);
86 // Add the CodeCompileUnit to the compilation
87 assemblyBuilder.AddCodeCompileUnit(this, codeCompileUnit);
89 throw new NotImplementedException("System.Data.Design - ROTORTODO");
90 #endif // !FEATURE_PAL